forked from fragglet/ipxbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipxbox.go
168 lines (154 loc) · 5.29 KB
/
ipxbox.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
// Package main implements a standalone DOSbox-IPX server.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/skadarnold/ipxbox/ipx"
"github.com/skadarnold/ipxbox/ipxpkt"
"github.com/skadarnold/ipxbox/network"
"github.com/skadarnold/ipxbox/network/addressable"
"github.com/skadarnold/ipxbox/network/filter"
"github.com/skadarnold/ipxbox/network/ipxswitch"
"github.com/skadarnold/ipxbox/network/stats"
"github.com/skadarnold/ipxbox/network/tappable"
"github.com/skadarnold/ipxbox/phys"
"github.com/skadarnold/ipxbox/ppp/pptp"
"github.com/skadarnold/ipxbox/qproxy"
"github.com/skadarnold/ipxbox/server"
"github.com/skadarnold/ipxbox/server/dosbox"
"github.com/skadarnold/ipxbox/server/uplink"
"github.com/skadarnold/ipxbox/syslog"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)
var (
dumpPackets = flag.String("dump_packets", "", "Write packets to a .pcap file with the given name.")
port = flag.Int("port", 10000, "UDP port to listen on.")
clientTimeout = flag.Duration("client_timeout", 10*time.Minute, "Time of inactivity before disconnecting clients.")
allowNetBIOS = flag.Bool("allow_netbios", false, "If true, allow packets to be forwarded that may contain Windows file sharing (NetBIOS) packets.")
enableIpxpkt = flag.Bool("enable_ipxpkt", false, "If true, route encapsulated packets from the IPXPKT.COM driver to the physical network (requires --enable_tap or --pcap_device)")
enableSyslog = flag.Bool("enable_syslog", false, "If true, client connects/disconnects are logged to syslog")
quakeServers = flag.String("quake_servers", "", "Proxy to the given list of Quake UDP servers in a way that makes them accessible over IPX.")
enablePPTP = flag.Bool("enable_pptp", false, "If true, run PPTP VPN server on TCP port 1723.")
uplinkPassword = flag.String("uplink_password", "", "Password to permit uplink clients to connect. If empty, uplink is not supported.")
)
func addQuakeProxies(ctx context.Context, net network.Network) {
if *quakeServers == "" {
return
}
for _, addr := range strings.Split(*quakeServers, ",") {
p := qproxy.New(&qproxy.Config{
Address: addr,
IdleTimeout: *clientTimeout,
}, net.NewNode())
go p.Run(ctx)
}
}
func makePcapWriter() *pcapgo.Writer {
f, err := os.Create(*dumpPackets)
if err != nil {
log.Fatalf("failed to open pcap file for write: %v", err)
}
w := pcapgo.NewWriter(f)
w.WriteFileHeader(1500, layers.LinkTypeEthernet)
return w
}
func makeNetwork(ctx context.Context) (network.Network, network.Network) {
// We build the network up in layers, each layer adding an extra
// feature. This approach allows for modularity and separation of
// concerns, avoiding the complexity of a big monolithic system.
// This is best read in reverse order. Life of an rx packet:
// 1. Packet received from client; WritePacket() by server
// 2. Check source address matches client address (addressable)
// 3. Increment receive statistics (stats)
// 4. Drop packet if a NetBIOS packet (filter)
// 5. Fork incoming traffic to any network taps (tappable)
// 6. Forward to receive queue(s) of other clients (ipxswitch)
// Then back out the other way (tx):
// 1. Read packet from receive queue (ipxswitch)
// 2. No-op (tappable)
// 3. Filter NetBIOS packets (filter)
// 4. Increment transmit statistics (stats)
// 5. Check dest address matches client address (addressable)
// 5. ReadPacket() by server, and transmit to client.
var net network.Network
net = ipxswitch.New()
if *dumpPackets != "" {
tappableLayer := tappable.Wrap(net)
w := makePcapWriter()
sink := phys.NewPcapgoSink(w, phys.FramerEthernetII)
go ipx.CopyPackets(ctx, tappableLayer.NewTap(), sink)
net = tappableLayer
}
if !*allowNetBIOS {
net = filter.Wrap(net)
}
uplinkable := net
net = addressable.Wrap(net)
net = stats.Wrap(net)
return net, stats.Wrap(uplinkable)
}
func main() {
physFlags := phys.RegisterFlags()
flag.Parse()
ctx := context.Background()
var logger *log.Logger
if *enableSyslog {
var err error
logger, err = syslog.NewLogger(
syslog.LOG_NOTICE|syslog.LOG_DAEMON, 0)
if err != nil {
log.Fatalf("failed to init syslog: %v", err)
}
}
net, uplinkable := makeNetwork(ctx)
physLink, err := physFlags.MakePhys(*enableIpxpkt)
if err != nil {
log.Fatalf("failed to set up physical network: %v", err)
} else if physLink != nil {
port := uplinkable.NewNode()
go physLink.Run()
go ipx.DuplexCopyPackets(ctx, physLink, port)
if *enableIpxpkt {
r := ipxpkt.NewRouter(net.NewNode())
go phys.CopyFrames(r, physLink.NonIPX())
}
}
addQuakeProxies(ctx, net)
if *enablePPTP {
pptps, err := pptp.NewServer(net)
if err != nil {
log.Fatalf("failed to start PPTP server: %v", err)
}
go pptps.Run(ctx)
}
protocols := []server.Protocol{
&dosbox.Protocol{
Logger: logger,
Network: net,
KeepaliveTime: 5 * time.Second,
},
}
if *uplinkPassword != "" {
protocols = append(protocols, &uplink.Protocol{
Logger: logger,
Network: uplinkable,
Password: *uplinkPassword,
KeepaliveTime: 5 * time.Second,
})
}
s, err := server.New(fmt.Sprintf(":%d", *port), &server.Config{
Protocols: protocols,
ClientTimeout: *clientTimeout,
Logger: logger,
})
if err != nil {
log.Fatal(err)
}
s.Run(ctx)
}