-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (50 loc) · 1.21 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
//go:build linux
package main
import (
"context"
"errors"
"log/slog"
"net"
"os"
"syscall"
"github.com/things-go/go-socks5"
"golang.org/x/sys/unix"
)
func main() {
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
port := os.Getenv("PP_SOCKS5_PORT")
if port == "" {
port = "1080"
}
lc := net.ListenConfig{}
l, err := lc.Listen(context.Background(), "tcp", ":"+port)
if err != nil {
panic(err)
}
log.Info("start server")
dialer := net.Dialer{
ControlContext: func(ctx context.Context, network, address string, c syscall.RawConn) error {
log.Debug("new connection", "network", network, "address", address)
var innerErr error
outerErr := c.Control(func(fd uintptr) {
innerErr = syscall.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_MARK, int(uint32(0x163)))
})
err := errors.Join(outerErr, innerErr)
if err != nil {
log.Error("failed to set socket options", "err", err)
}
return err
},
}
s := socks5.NewServer(
socks5.WithDial(dialer.DialContext),
)
// Create SOCKS5 proxy on localhost port 8000
for {
if err := s.Serve(l); err != nil {
log.Error("failed to accept new connection", "error", err)
}
}
}