-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (153 loc) · 4.07 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"mrproxy/common"
"mrproxy/dns"
"mrproxy/listener"
"mrproxy/proxy"
_ "mrproxy/proxy/freedom"
_ "mrproxy/proxy/slow"
_ "mrproxy/proxy/socks5"
"mrproxy/transport"
"net"
"os"
"os/signal"
"syscall"
"time"
)
type Hub struct {
listener proxy.Listener
}
// func Start() error {
// ctx := context.Background()
// hub, err := internet.ListenTCP(ctx, w.address, w.port, w.stream, func(conn stat.Connection) {
// go w.callback(conn)
// })
// if err != nil {
// return newError("failed to listen TCP on ", w.port).AtWarning().Base(err)
// }
// w.hub = hub
// return nil
// }
func main() {
//init work dir
common.SetWorkDir()
// init Log
var programLevel = new(slog.LevelVar)
h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: programLevel})
slog.SetDefault(slog.New(h))
programLevel.Set(slog.LevelDebug)
var inNode string
var outNode string
var configFile string
// 给命令行参数绑定变量
flag.StringVar(&configFile, "c", "", "config file path")
// 解析命令行参数
flag.Parse()
// 初始化配置
common.InitGlobalConfig(configFile)
inNode = common.GetConfig().GetNode("inNode").(map[string]any)["protocol"].(string)
outNode = common.GetConfig().GetNode("outNode").(map[string]any)["protocol"].(string)
//TODO 配置中心
listen := common.GetConfig().GetNode("inNode").(map[string]any)["listen"].(string)
port := common.GetConfig().GetNode("inNode").(map[string]any)["port"].(float64)
//port =
listenAddr := common.NewTargetAddress()
listenAddr.SetNetwork("tcp")
listenAddr.SetAddress(listen)
listenAddr.SetPort(int(port))
server, _ := proxy.ServerMap[inNode]()
client, _ := proxy.ClientMap[outNode]()
freedom, _ := proxy.ClientMap["freedom"]()
dnsQuery := dns.NewLocalQuery()
cache := common.NewKVCache(time.Second * 3600 * 8)
_, err := listener.ListenTCP(context.TODO(), listenAddr, nil, func(conn net.Conn) {
//slog.Debug(fmt.Sprintf("new conn from %s", conn.RemoteAddr()))
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
slog.Warn(fmt.Sprintf("close lc conn error,err=%s", err.Error()))
}
}(conn)
lc, remoteAddr, err := server.Handle(conn)
if err != nil {
slog.Warn(fmt.Sprintf("server.Handle(conn) error,err=%s", err.Error()))
return
}
//TODO 分流
ip := net.ParseIP(remoteAddr.GetAddress())
switch ip {
//domain
case nil:
if val, ok := cache.Get(remoteAddr.GetAddress()); ok {
slog.Debug(fmt.Sprintf("get cache for %s:", remoteAddr.GetAddress()))
ip = net.ParseIP(val.(string))
break
}
ips, err := dnsQuery.LookupIP(remoteAddr.GetAddress(), "ip4")
if err != nil {
ip = net.ParseIP("8.8.8.8")
break
}
ip = net.ParseIP(ips[0])
cache.Set(remoteAddr.GetAddress(), ip.String())
default:
//ip
}
mmdb := common.Instance()
record, err1 := mmdb.Country(ip)
if err1 != nil {
return
}
code := record.Country.IsoCode
var clientSwitch proxy.OutNode
clientSwitch = freedom
slog.Info(fmt.Sprintf("[%s] ===> ioscode[%s]", remoteAddr.GetAddress(), code))
mode := "Rule"
switch mode {
case "Rule":
if code != "CN" {
clientSwitch = client
}
if code == "" {
clientSwitch = client
}
case "Global":
clientSwitch = client
default:
clientSwitch = freedom
}
if clientSwitch == client {
slog.Info(fmt.Sprintf("[%s] ===> choose proxy", remoteAddr.GetAddress()))
} else {
slog.Info(fmt.Sprintf("[%s] ===> choose direct", remoteAddr.GetAddress()))
}
rc, err := clientSwitch.Handle(conn, remoteAddr, transport.NewTcpDialer())
if err != nil {
slog.Info(fmt.Sprintf("client handle error =%s", err.Error()))
return
}
defer func(rc net.Conn) {
err := rc.Close()
if err != nil {
slog.Warn(fmt.Sprintf("close rc conn error,err=%s", err.Error()))
return
}
}(rc)
//lc是浏览器 rc是要代理访问的远程端
//common.Relay(lc, rc)
common.Relay2(lc, rc)
})
if err != nil {
return
}
{
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
<-osSignals
cache.StopCleanup()
}
}