This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
217 lines (187 loc) · 3.72 KB
/
server.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package secretun
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
)
type userConfig struct {
Users string
}
type natConfig struct {
Net string
Gateway string
Mtu int
}
type Server struct {
cfg Config
user_cfg userConfig
nat_cfg natConfig
tunnel_cfg Config
tunnel ServerTunnel
ippool IPPool
}
func NewServer(cfg Config) (ser Server, err error) {
ser.cfg = cfg
if pkg_cfg, e := cfg.GetConfig("packet"); e != nil {
err = e
return
} else if err = InitPacket(pkg_cfg); err != nil {
return
}
if err = cfg.Get("auth", &ser.user_cfg); err != nil {
return
}
if err = cfg.Get("nat", &ser.nat_cfg); err != nil {
return
}
if ser.ippool, err = NewIPPool(ser.nat_cfg.Net, ser.nat_cfg.Gateway); err != nil {
return
}
var tunnel_name string
if ser.tunnel_cfg, err = cfg.GetConfig("tunnel"); err != nil {
return
} else if err = ser.tunnel_cfg.Get("name", &tunnel_name); err != nil {
return
}
ser.tunnel, err = NewServerTunnel(tunnel_name)
return
}
func (s *Server) Init() error {
return s.tunnel.Init(s.tunnel_cfg)
}
func (s *Server) Run() error {
for {
if cli_ch, err := s.tunnel.Accept(); err != nil {
return err
} else {
go s.handle_client(cli_ch)
}
}
return nil
}
func (s *Server) Shutdown() error {
return nil
}
func (s *Server) handle_client(cli_ch ClientChan) {
defer cli_ch.Close()
nat_info, err := s.auth(&cli_ch)
if err != nil {
log.Println(err)
return
}
if err = s.nat(&cli_ch, nat_info); err != nil {
log.Println(err)
}
}
func (s *Server) auth(cli_ch *ClientChan) (nf NatInfo, err error) {
var auth_info AuthInfo
var rst AuthResult
p := <-cli_ch.R
if p.Decode(&auth_info) != nil {
err = fmt.Errorf("invalid auth info")
return
}
if !s.check_user(&auth_info) {
rst.Ok = false
err = fmt.Errorf("invalid user")
} else if s.ippool.IsEmpty() {
rst.Ok = false
rst.Message = "ip used up"
err = fmt.Errorf("ip used up")
} else {
rst.Ok = true
rst.NatInfo.Gateway = s.ippool.Gateway
rst.NatInfo.Netmask = s.ippool.IPNet.Mask
rst.NatInfo.IP = s.ippool.Next()
rst.NatInfo.MTU = s.nat_cfg.Mtu
nf = rst.NatInfo
}
if p.Encode(&rst) != nil {
err = fmt.Errorf("encode AuthResult fail")
return
}
cli_ch.W <- p
return
}
func (s *Server) nat(cli_ch *ClientChan, nat_info NatInfo) error {
tun, err := CreateTun("")
if err != nil {
return err
}
defer tun.Close()
if err := tun.SetAddr(nat_info.Gateway, nat_info.IP); err != nil {
return err
}
if err := tun.SetNetmask(nat_info.Netmask); err != nil {
return err
}
if s.nat_cfg.Mtu > 0 {
if err := tun.SetMTU(s.nat_cfg.Mtu); err != nil {
return err
}
}
tun_ch, err := tun.ReadChan()
if err != nil {
return err
}
if err := tun.Up(); err != nil {
return err
}
for {
select {
case packet, ok := <-cli_ch.R:
if !ok {
log.Println("tunnel closed")
return nil
}
if packet.Type == PT_P2P {
if _, err := tun.Write(packet.Data); err != nil {
return nil
}
} else {
return nil
}
case data, ok := <-tun_ch:
if !ok {
log.Println("chan closed")
return nil
}
p := NewPacket(PT_P2P, data)
cli_ch.W <- p
case err := <-cli_ch.End:
return err
}
}
return nil
}
func (s *Server) check_user(info *AuthInfo) bool {
f, err := os.Open(s.user_cfg.Users)
if err != nil {
return false
}
defer f.Close()
buf := bufio.NewReader(f)
for {
line, err := buf.ReadString('\n')
if err != nil && err != io.EOF {
break
}
if len(line) == 0 || line[0] == '#' {
continue
}
segs := strings.Split(line, " ")
if len(segs) != 2 {
continue
}
if segs[1][len(segs[1])-1] == '\n' {
segs[1] = segs[1][:len(segs[1])-1]
}
if string(segs[0]) == info.Username {
return string(segs[1]) == info.Password
}
}
return false
}