-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
150 lines (126 loc) · 3.35 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
_ "embed"
"fmt"
"log"
"log/slog"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"path"
"github.com/DaniilSokolyuk/go-pcap2socks/cfg"
"github.com/DaniilSokolyuk/go-pcap2socks/core"
"github.com/DaniilSokolyuk/go-pcap2socks/core/device"
"github.com/DaniilSokolyuk/go-pcap2socks/core/option"
"github.com/DaniilSokolyuk/go-pcap2socks/proxy"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
//go:embed config.json
var configData string
func main() {
// get config file from first argument or use config.json
var cfgFile string
if len(os.Args) > 1 {
cfgFile = os.Args[1]
} else {
executable, err := os.Executable()
if err != nil {
slog.Error("get executable error", "error", err)
return
}
cfgFile = path.Join(path.Dir(executable), "config.json")
}
cfgExists := cfg.Exists(cfgFile)
if !cfgExists {
slog.Info("Config file not found, creating a new one", "file", cfgFile)
//path to near executable file
err := os.WriteFile(cfgFile, []byte(configData), 0666)
if err != nil {
slog.Error("write config error", "file", cfgFile, "error", err)
return
}
}
config, err := cfg.Load(cfgFile)
if err != nil {
slog.Error("load config error", "file", cfgFile, "error", err)
return
}
slog.Info("Config loaded", "file", cfgFile)
if len(config.ExecuteOnStart) > 0 {
slog.Info("Executing commands on start", "cmd", config.ExecuteOnStart)
var cmd *exec.Cmd
if len(config.ExecuteOnStart) > 1 {
cmd = exec.Command(config.ExecuteOnStart[0], config.ExecuteOnStart[1:]...)
} else {
cmd = exec.Command(config.ExecuteOnStart[0])
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
err := cmd.Start()
if err != nil {
slog.Error("execute command error", "error", err)
}
err = cmd.Wait()
if err != nil {
}
}()
}
err = run(config)
if err != nil {
slog.Error("run error", "error", err)
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
func run(cfg *cfg.Config) error {
proxies := make(map[string]proxy.Proxy)
var err error
for _, outbound := range cfg.Outbounds {
var p proxy.Proxy
switch {
case outbound.Direct != nil:
p = proxy.NewDirect()
case outbound.Socks != nil:
p, err = proxy.NewSocks5(outbound.Socks.Address, outbound.Socks.Username, outbound.Socks.Password)
if err != nil {
return fmt.Errorf("new socks5 error: %w", err)
}
case outbound.Reject != nil:
p = proxy.NewReject()
default:
return fmt.Errorf("invalid outbound: %+v", outbound)
}
proxies[outbound.Tag] = p
}
_defaultProxy = proxy.NewRouter(cfg.Routing.Rules, proxies)
proxy.SetDialer(_defaultProxy)
_defaultDevice, err = device.Open(cfg.PCAP, func() device.Stacker {
return _defaultStack
})
if err != nil {
return err
}
if _defaultStack, err = core.CreateStack(&core.Config{
LinkEndpoint: _defaultDevice,
TransportHandler: &core.Tunnel{},
MulticastGroups: []net.IP{},
Options: []option.Option{},
}); err != nil {
slog.Error("create stack error: %w", err)
}
return nil
}
var (
// _defaultProxy holds the default proxy for the engine.
_defaultProxy proxy.Proxy
// _defaultDevice holds the default device for the engine.
_defaultDevice device.Device
// _defaultStack holds the default stack for the engine.
_defaultStack *stack.Stack
)