forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packetman.go
199 lines (160 loc) · 5.86 KB
/
packetman.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
/*
* Copyright (c) 2020, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"net"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/packetman"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
)
func makePacketManipulatorConfig(
support *SupportServices) (*packetman.Config, error) {
// Packet interception is configured for any tunnel protocol port that _may_
// use packet manipulation. A future hot reload of tactics may apply specs to
// any of these protocols.
var ports []int
for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
if protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
ports = append(ports, port)
}
}
selectSpecName := func(protocolPort int, clientIP net.IP) (string, interface{}) {
specName, extraData, err := selectPacketManipulationSpec(
support, protocolPort, clientIP)
if err != nil {
log.WithTraceFields(
LogFields{"error": err}).Warning(
"failed to get tactics for packet manipulation")
return "", nil
}
return specName, extraData
}
specs, err := getPacketManipulationSpecs(support)
if err != nil {
return nil, errors.Trace(err)
}
return &packetman.Config{
Logger: CommonLogger(log),
SudoNetworkConfigCommands: support.Config.PacketTunnelSudoNetworkConfigCommands,
QueueNumber: 1,
ProtocolPorts: ports,
Specs: specs,
SelectSpecName: selectSpecName,
}, nil
}
func getPacketManipulationSpecs(support *SupportServices) ([]*packetman.Spec, error) {
// By convention, parameters.ServerPacketManipulationSpecs should be in
// DefaultTactics, not FilteredTactics; and ServerTacticsParametersCache
// ignores Tactics.Probability.
p, err := support.ServerTacticsParametersCache.Get(NewGeoIPData())
if err != nil {
return nil, errors.Trace(err)
}
if p.IsNil() {
// No tactics are configured; return an empty spec list.
return nil, nil
}
paramSpecs := p.PacketManipulationSpecs(parameters.ServerPacketManipulationSpecs)
specs := make([]*packetman.Spec, len(paramSpecs))
for i, spec := range paramSpecs {
packetmanSpec := packetman.Spec(*spec)
specs[i] = &packetmanSpec
}
return specs, nil
}
func reloadPacketManipulationSpecs(support *SupportServices) error {
specs, err := getPacketManipulationSpecs(support)
if err != nil {
return errors.Trace(err)
}
err = support.PacketManipulator.SetSpecs(specs)
if err != nil {
return errors.Trace(err)
}
return nil
}
func selectPacketManipulationSpec(
support *SupportServices,
protocolPort int,
clientIP net.IP) (string, interface{}, error) {
// First check for replay, then check tactics.
// The intercepted packet source/protocol port is used to determine the
// tunnel protocol name, which is used to lookup first replay and then
// enabled packet manipulation specs in ServerProtocolPacketManipulations.
//
// This assumes that all TunnelProtocolMayUseServerPacketManipulation
// protocols run on distinct ports, which is true when all such protocols run
// over TCP.
targetTunnelProtocol := ""
for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
if port == protocolPort && protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
targetTunnelProtocol = tunnelProtocol
break
}
}
if targetTunnelProtocol == "" {
return "", nil, errors.Tracef(
"packet manipulation protocol port not found: %d", protocolPort)
}
geoIPData := support.GeoIPService.Lookup(clientIP.String())
specName, doReplay := support.ReplayCache.GetReplayPacketManipulation(
targetTunnelProtocol, geoIPData)
// extraData records the is_server_replay metric.
extraData := doReplay
if doReplay {
return specName, extraData, nil
}
// GeoIP tactics filtering is applied when getting
// ServerPacketManipulationProbability and ServerProtocolPacketManipulations.
//
// When there are multiple enabled specs, one is selected at random.
//
// Specs under the key "All" apply to all protocols. Duplicate specs per
// entry are allowed, enabling weighted selection. If a spec appears in both
// "All" and a specific protocol, the duplicate(s) are retained.
p, err := support.ServerTacticsParametersCache.Get(geoIPData)
if err != nil {
return "", nil, errors.Trace(err)
}
if p.IsNil() {
// No tactics are configured; select no spec.
return "", extraData, nil
}
if !p.WeightedCoinFlip(parameters.ServerPacketManipulationProbability) {
return "", extraData, nil
}
protocolSpecs := p.ProtocolPacketManipulations(
parameters.ServerProtocolPacketManipulations)
// TODO: cache merged per-protocol + "All" lists?
specNames, ok := protocolSpecs[targetTunnelProtocol]
if !ok {
specNames = []string{}
}
allProtocolsSpecNames, ok := protocolSpecs[protocol.TUNNEL_PROTOCOLS_ALL]
if ok {
specNames = append(specNames, allProtocolsSpecNames...)
}
if len(specNames) < 1 {
// Tactics contains no candidate specs for this protocol.
return "", extraData, nil
}
return specNames[prng.Range(0, len(specNames)-1)], extraData, nil
}