-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (158 loc) · 6.12 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
174
// Copyright 2019 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"time"
oss "github.com/ernestzhang/outline-go-tun2socks/outline/shadowsocks"
"github.com/ernestzhang/outline-go-tun2socks/shadowsocks"
"github.com/eycorsican/go-tun2socks/common/log"
_ "github.com/eycorsican/go-tun2socks/common/log/simple" // Register a simple logger.
"github.com/eycorsican/go-tun2socks/core"
"github.com/eycorsican/go-tun2socks/proxy/dnsfallback"
"github.com/eycorsican/go-tun2socks/tun"
)
const (
mtu = 1500
udpTimeout = 30 * time.Second
persistTun = true // Linux: persist the TUN interface after the last open file descriptor is closed.
)
var args struct {
tunAddr *string
tunGw *string
tunMask *string
tunName *string
tunDNS *string
proxyHost *string
proxyPort *int
proxyPassword *string
proxyCipher *string
logLevel *string
checkConnectivity *bool
dnsFallback *bool
version *bool
tk *string
tcp_website *string
udp_dns *string
}
var version string // Populated at build time through `-X main.version=...`
var lwipWriter io.Writer
func main() {
args.tunAddr = flag.String("tunAddr", "10.0.85.2", "TUN interface IP address")
args.tunGw = flag.String("tunGw", "10.0.85.1", "TUN interface gateway")
args.tunMask = flag.String("tunMask", "255.255.255.0", "TUN interface network mask; prefixlen for IPv6")
args.tunDNS = flag.String("tunDNS", "1.1.1.1,9.9.9.9,208.67.222.222", "Comma-separated list of DNS resolvers for the TUN interface (Windows only)")
args.tunName = flag.String("tunName", "tun0", "TUN interface name")
args.proxyHost = flag.String("proxyHost", "", "Shadowsocks proxy hostname or IP address")
args.proxyPort = flag.Int("proxyPort", 0, "Shadowsocks proxy port number")
args.proxyPassword = flag.String("proxyPassword", "", "Shadowsocks proxy password")
args.proxyCipher = flag.String("proxyCipher", "chacha20-ietf-poly1305", "Shadowsocks proxy encryption cipher")
args.logLevel = flag.String("logLevel", "info", "Logging level: debug|info|warn|error|none")
args.dnsFallback = flag.Bool("dnsFallback", false, "Enable DNS fallback over TCP (overrides the UDP handler).")
args.checkConnectivity = flag.Bool("checkConnectivity", false, "Check the proxy TCP and UDP connectivity and exit.")
args.version = flag.Bool("version", false, "Print the version and exit.")
args.tk = flag.String("tk", "123", "token ...")
args.tcp_website = flag.String("tcp_website", "http://google.com", "need website")
args.udp_dns = flag.String("udp_dns", "8.8.8.8:53", "need dns svr")
flag.Parse()
if *args.version {
fmt.Println(version)
os.Exit(0)
}
setLogLevel(*args.logLevel)
// Validate proxy flags
if *args.proxyHost == "" {
log.Errorf("Must provide a Shadowsocks proxy host name or IP address")
os.Exit(oss.IllegalConfiguration)
} else if *args.proxyPort <= 0 || *args.proxyPort > 65535 {
log.Errorf("Must provide a valid Shadowsocks proxy port [1:65535]")
os.Exit(oss.IllegalConfiguration)
} else if *args.proxyPassword == "" {
log.Errorf("Must provide a Shadowsocks proxy password")
os.Exit(oss.IllegalConfiguration)
} else if *args.proxyCipher == "" {
log.Errorf("Must provide a Shadowsocks proxy encryption cipher")
os.Exit(oss.IllegalConfiguration)
}/*else if *args.tk == "" {//|| len(*args.tk) != 32 {
log.Errorf("wrong token...")
os.Exit(oss.IllegalConfiguration)
}*/
tk := *args.tk
tcp_website := *args.tcp_website
udp_dns := *args.udp_dns
if *args.checkConnectivity {
connErrCode, err := oss.CheckConnectivity(*args.proxyHost, *args.proxyPort, *args.proxyPassword, *args.proxyCipher , tk , tcp_website , udp_dns)
log.Debugf("Connectivity checks error code: %v", connErrCode)
if err != nil {
log.Errorf("Failed to perform connectivity checks: %v", err)
}
os.Exit(connErrCode)
}
// Open TUN device
dnsResolvers := strings.Split(*args.tunDNS, ",")
tunDevice, err := tun.OpenTunDevice(*args.tunName, *args.tunAddr, *args.tunGw, *args.tunMask, dnsResolvers, persistTun)
if err != nil {
log.Errorf("Failed to open TUN device: %v", err)
os.Exit(oss.SystemMisconfigured)
}
// Output packets to TUN device
core.RegisterOutputFn(tunDevice.Write)
// Register TCP and UDP connection handlers
core.RegisterTCPConnHandler(
shadowsocks.NewTCPHandler(*args.proxyHost, *args.proxyPort, *args.proxyPassword, *args.proxyCipher , tk))
if *args.dnsFallback {
// UDP connectivity not supported, fall back to DNS over TCP.
log.Debugf("Registering DNS fallback UDP handler")
core.RegisterUDPConnHandler(dnsfallback.NewUDPHandler())
} else {
core.RegisterUDPConnHandler(
shadowsocks.NewUDPHandler(*args.proxyHost, *args.proxyPort, *args.proxyPassword, *args.proxyCipher, udpTimeout , tk))
}
// Configure LWIP stack to receive input data from the TUN device
lwipWriter := core.NewLWIPStack()
go func() {
_, err := io.CopyBuffer(lwipWriter, tunDevice, make([]byte, mtu))
if err != nil {
log.Errorf("Failed to write data to network stack: %v", err)
os.Exit(oss.Unexpected)
}
}()
log.Infof("tun2socks running...")
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP)
sig := <-osSignals
log.Debugf("Received signal: %v", sig)
}
func setLogLevel(level string) {
switch strings.ToLower(level) {
case "debug":
log.SetLevel(log.DEBUG)
case "info":
log.SetLevel(log.INFO)
case "warn":
log.SetLevel(log.WARN)
case "error":
log.SetLevel(log.ERROR)
case "none":
log.SetLevel(log.NONE)
default:
log.SetLevel(log.INFO)
}
}