forked from AlekSi/telesock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (152 loc) · 4.02 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
175
176
177
178
179
180
// telesock - Fast and simple SOCKS5 proxy.
// Written in 2018 by Alexey Palazhchenko.
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights
// to this software to the public domain worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
package main
import (
"context"
"io/ioutil"
"net"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
"time"
"go.uber.org/zap"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
"github.com/AlekSi/telesock/internal"
)
func runTCPConn(ctx context.Context, c *net.TCPConn, l *zap.SugaredLogger, conf *internal.Config) {
tcp := internal.NewTCPConn(c, l, conf)
defer tcp.Close()
if !tcp.Auth(ctx) {
return
}
if !tcp.Req(ctx) {
return
}
tcp.Run(ctx)
}
func runTCPListener(ctx context.Context, addr string, l *zap.SugaredLogger, conf *internal.Config) {
tcp, err := net.Listen("tcp", addr)
if err != nil {
l.Error(err)
return
}
go func() {
<-ctx.Done()
tcp.Close()
l.Infof("Listener closed.")
}()
var wg sync.WaitGroup
l.Infof("Listener started on %s.", tcp.Addr())
for {
c, err := tcp.Accept()
if err != nil {
// are we done?
if ctx.Err() != nil {
break
}
// wait a little before next accept attempt to give OS a chance to free resources
l.Error(err)
time.Sleep(100 * time.Millisecond)
continue
}
conn := c.(*net.TCPConn)
if err = conn.SetReadBuffer(4096); err != nil {
l.Warn(err)
}
if err = conn.SetWriteBuffer(4096); err != nil {
l.Warn(err)
}
wg.Add(1)
go runTCPConn(ctx, conn, l.With(zap.String("client", c.RemoteAddr().String())), conf)
}
wg.Wait()
}
func loadConfig(path string, l *zap.SugaredLogger, port string) *internal.Config {
// read and parse config
b, err := ioutil.ReadFile(path)
if err != nil {
l.Fatalf("Can't read configuration file: %s.", err)
}
var config internal.Config
if err = yaml.UnmarshalStrict(b, &config); err != nil {
l.Fatalf("Can't read configuration: %s.", err)
}
l.Infof("Loaded %d users.", len(config.Users))
if config.Server == "" {
return &config
}
u := &url.URL{
Scheme: "https",
Host: "t.me",
Path: "socks",
}
for _, user := range config.Users {
q := make(url.Values)
q.Set("server", config.Server)
q.Set("port", port)
q.Set("user", user.Username)
q.Set("pass", user.Password)
u.RawQuery = q.Encode()
l.Infof("%20s: %s", user.Username, u.String())
}
return &config
}
func main() {
// parse flags
tcpListenF := kingpin.Flag("tcp-listen", "TCP address to listen").Default(":1080").String()
configF := kingpin.Flag("config", "Config file name").Default("telesock.yaml").String()
verboseF := kingpin.Flag("verbose", "Log INFO level log messages").Bool()
debugF := kingpin.Flag("debug", "Log DEBUG level log messages (implies --verbose)").Bool()
kingpin.Parse()
// setup logger
loggerConfig := zap.NewDevelopmentConfig()
loggerConfig.DisableStacktrace = true
logger, err := loggerConfig.Build()
if err != nil {
panic(err)
}
l := logger.Sugar()
defer l.Sync()
_, port, err := net.SplitHostPort(*tcpListenF)
if err != nil {
l.Fatal(err)
}
config := loadConfig(*configF, l, port)
// set logger level after config is parsed
switch {
case *debugF:
loggerConfig.Level.SetLevel(zap.DebugLevel)
case *verboseF:
loggerConfig.Level.SetLevel(zap.InfoLevel)
default:
loggerConfig.Level.SetLevel(zap.WarnLevel)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// handle termination signals
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
go func() {
s := <-signals
signal.Stop(signals)
l.Warnf("Got %v (%d) signal, shutting down...", s, s)
cancel()
}()
var wg sync.WaitGroup
// start TCP listener
wg.Add(1)
go func() {
defer wg.Done()
runTCPListener(ctx, *tcpListenF, l.With(zap.String("component", "tcp")), config)
}()
wg.Wait()
}