-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
125 lines (109 loc) · 3.25 KB
/
service.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
package anytls
import (
"context"
"crypto/sha256"
"encoding/binary"
"errors"
"net"
"os"
"github.com/anytls/sing-anytls/padding"
"github.com/anytls/sing-anytls/session"
"github.com/sagernet/sing/common/atomic"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
type Service struct {
users map[[32]byte]string
padding atomic.TypedValue[*padding.PaddingFactory]
handler N.TCPConnectionHandlerEx
fallbackHandler N.TCPConnectionHandlerEx
logger logger.ContextLogger
}
type ServiceConfig struct {
PaddingScheme []byte
Users []User
Handler N.TCPConnectionHandlerEx
FallbackHandler N.TCPConnectionHandlerEx
Logger logger.ContextLogger
}
type User struct {
Name string
Password string
}
func NewService(config ServiceConfig) (*Service, error) {
service := &Service{
handler: config.Handler,
fallbackHandler: config.FallbackHandler,
logger: config.Logger,
users: make(map[[32]byte]string),
}
if service.handler == nil || service.logger == nil {
return nil, os.ErrInvalid
}
for _, user := range config.Users {
service.users[sha256.Sum256([]byte(user.Password))] = user.Name
}
if !padding.UpdatePaddingScheme(config.PaddingScheme, &service.padding) {
return nil, errors.New("incorrect padding scheme format")
}
return service, nil
}
// NewConnection `conn` should be plaintext
func (s *Service) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, onClose N.CloseHandlerFunc) error {
b := buf.NewPacket()
defer b.Release()
_, err := b.ReadOnceFrom(conn)
if err != nil {
return err
}
conn = bufio.NewCachedConn(conn, b)
by, err := b.ReadBytes(32)
if err != nil {
b.Reset()
return s.fallback(ctx, conn, source, err, onClose)
}
var passwordSha256 [32]byte
copy(passwordSha256[:], by)
if user, ok := s.users[passwordSha256]; ok {
ctx = auth.ContextWithUser(ctx, user)
} else {
b.Reset()
return s.fallback(ctx, conn, source, E.New("unknown user password"), onClose)
}
by, err = b.ReadBytes(2)
if err != nil {
b.Reset()
return s.fallback(ctx, conn, source, E.Extend(err, "read padding length"), onClose)
}
paddingLen := binary.BigEndian.Uint16(by)
if paddingLen > 0 {
_, err = b.ReadBytes(int(paddingLen))
if err != nil {
b.Reset()
return s.fallback(ctx, conn, source, E.Extend(err, "read padding"), onClose)
}
}
session := session.NewServerSession(conn, func(stream *session.Stream) {
destination, err := M.SocksaddrSerializer.ReadAddrPort(stream)
if err != nil {
s.logger.ErrorContext(ctx, "ReadAddrPort:", err)
return
}
s.handler.NewConnectionEx(ctx, stream, source, destination, onClose)
}, &s.padding, s.logger)
session.Run()
session.Close()
return nil
}
func (s *Service) fallback(ctx context.Context, conn net.Conn, source M.Socksaddr, err error, onClose N.CloseHandlerFunc) error {
if s.fallbackHandler == nil {
return E.Extend(err, "fallback disabled")
}
s.fallbackHandler.NewConnectionEx(ctx, conn, source, M.Socksaddr{}, onClose)
return nil
}