-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.go
147 lines (118 loc) · 3.06 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
package ultraviolet
import (
"net"
"time"
"github.com/pires/go-proxyproto"
"github.com/realDragonium/Ultraviolet/config"
"github.com/realDragonium/Ultraviolet/core"
"github.com/realDragonium/Ultraviolet/mc"
"github.com/realDragonium/Ultraviolet/module"
)
var botLimiter = module.NewBotFilterConnLimiter(5, time.Minute, time.Hour, 5*time.Second, mc.Packet{})
type ProxyAllServer struct {
}
func (s ProxyAllServer) ConnAction(req core.RequestData) core.ServerAction {
return core.PROXY
}
func (s ProxyAllServer) CreateConn(req core.RequestData) (net.Conn, error) {
return &net.TCPConn{}, nil
}
func (s ProxyAllServer) Status() mc.Packet {
return mc.Packet{}
}
func NewAPIServer(cfg config.APIServerConfig) core.Server {
dialTimeout, err := time.ParseDuration(cfg.DialTimeout)
if err != nil {
dialTimeout = time.Second
}
dialer := net.Dialer{
Timeout: dialTimeout,
LocalAddr: &net.TCPAddr{
IP: net.ParseIP(cfg.ProxyBind),
},
}
disconnectPacket := mc.ClientBoundDisconnect{
Reason: mc.String(cfg.DisconnectMessage),
}.Marshal()
cachedStatusPk := cfg.CachedStatus.Marshal()
serverState := core.Offline
if cfg.IsOnline {
serverState = core.Online
}
return APIServer{
sendProxyProtocol: cfg.SendProxyProtocol,
disconnectPacket: disconnectPacket,
dialer: dialer,
useStatusCache: cfg.UseStatusCache,
serverStatusPk: cachedStatusPk,
serverStatus: serverState,
useBotLimiter: cfg.LimitBots,
}
}
type APIServer struct {
sendProxyProtocol bool
disconnectPacket mc.Packet
dialer net.Dialer
proxyTo string
useBotLimiter bool
useStatusCache bool
serverStatusPk mc.Packet
serverStatus core.ServerState
}
func (server APIServer) ConnAction(req core.RequestData) core.ServerAction {
if server.useBotLimiter && !botLimiter.Allow(req) {
return core.VERIFY_CONN
}
switch server.serverStatus {
case core.Offline:
return server.serverOffline(req)
case core.Online:
return server.serverOnline(req)
default:
return core.CLOSE
}
}
func (server APIServer) serverOffline(req core.RequestData) core.ServerAction {
if req.Type == mc.Status {
return core.STATUS
}
return core.CLOSE
}
func (server APIServer) serverOnline(req core.RequestData) core.ServerAction {
if req.Type == mc.Login {
// if cfgServer.useRealipv2_4 {
// return PROXY_REALIP_2_4
// }
// if cfgServer.useRealipv2_5 {
// return PROXY_REALIP_2_5
// }
return core.PROXY
}
if req.Type == mc.Status {
if server.useStatusCache {
return core.STATUS
}
return core.PROXY
}
return core.CLOSE
}
func (server APIServer) CreateConn(req core.RequestData) (conn net.Conn, err error) {
conn, err = server.dialer.Dial("tcp", server.proxyTo)
if err != nil {
return
}
if server.sendProxyProtocol {
header := &proxyproto.Header{
Version: 2,
Command: proxyproto.PROXY,
TransportProtocol: proxyproto.TCPv4,
SourceAddr: conn.LocalAddr(),
DestinationAddr: conn.RemoteAddr(),
}
_, err = header.WriteTo(conn)
}
return
}
func (server APIServer) Status() mc.Packet {
return server.serverStatusPk
}