-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
108 lines (84 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Package main implements resocks.
package main
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"github.com/RedTeamPentesting/kbtls"
"github.com/spf13/cobra"
)
var version = "build from source"
const (
// DefaultProxyPort is the port on which the SOCKS5 server is exposed by default.
DefaultProxyPort = 1080
// DefaultListenPort is the port to which the reverse TLS connection is established by default.
DefaultListenPort = 4080
// ConnectionKeyEnvVariable is the environment variable through which the default connection key can be set.
ConnectionKeyEnvVariable = "RESOCKS_KEY"
)
var (
defaultConnectionKey = ""
defaultConnectBackAddress = ""
)
func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run() error {
relayCmd := relayCommand()
listenCmd := listenCommand()
generateCmd := &cobra.Command{
Use: "generate",
Short: "Generates a connection key",
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
key, err := kbtls.GenerateConnectionKey()
if err != nil {
return err
}
fmt.Println(key.String())
return nil
},
}
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the current version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("resocks %s\n", version)
},
}
relayCmd.AddCommand(listenCmd)
relayCmd.AddCommand(generateCmd)
relayCmd.AddCommand(versionCmd)
return relayCmd.Execute()
}
func withDefaultPort(addr string, defaultPort int) string {
_, _, err := net.SplitHostPort(addr)
if err == nil {
return addr
}
return addr + ":" + strconv.Itoa(defaultPort)
}
func binaryName() string {
executable, err := os.Executable()
if err == nil {
return filepath.Base(executable)
}
if len(os.Args) > 0 {
return filepath.Base(os.Args[0])
}
return "resocks"
}
func fromEnvWithFallback(envVariable string, fallback string) string {
value, ok := os.LookupEnv(envVariable)
if !ok {
return fallback
}
return value
}