forked from EKyero/shadowsocks-multiuser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
62 lines (50 loc) · 1.17 KB
/
instance.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
package main
import (
"log"
"net"
"github.com/catding/shadowsocks-multiuser/core"
)
// Instance struct
type Instance struct {
UserID int
Port int
Method string
Password string
Bandwidth *Bandwidth
Started bool
TCPSocket net.Listener
UDPSocket net.PacketConn
}
// Start instance
func (instance *Instance) Start() {
cipher, err := core.PickCipher(instance.Method, make([]byte, 0), instance.Password)
if err != nil {
log.Println(err)
return
}
instance.Started = true
go tcpRemote(instance, cipher.StreamConn)
if flags.UDPEnabled {
go udpRemote(instance, cipher.PacketConn)
}
}
// Stop instance
func (instance *Instance) Stop() {
instance.Started = false
if instance.TCPSocket != nil {
instance.TCPSocket.Close()
}
if instance.UDPSocket != nil && flags.UDPEnabled {
instance.UDPSocket.Close()
}
}
func newInstance(id int, port int, method, password string) *Instance {
instance := Instance{}
instance.UserID = id
instance.Port = port
instance.Method = method
instance.Password = password
instance.Bandwidth = newBandwidth()
instance.Started = false
return &instance
}