-
Notifications
You must be signed in to change notification settings - Fork 0
/
faketun.go
96 lines (74 loc) · 2.13 KB
/
faketun.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
package main
import (
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"io"
"log"
"net"
"os"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/tun"
)
type FakeTun struct {
file io.ReadWriteCloser
clientIp tcpip.Address
sourceIp tcpip.Address
}
func (f *FakeTun) File() *os.File {
return f.file.(*os.File)
}
func (f *FakeTun) Read(bytes []byte, offset int) (int, error) {
//log.Println("Read")
i, err := f.file.Read(bytes[offset:])
if err != nil {
return 0, err
}
//packetInfo(bytes[offset:])
hdr := header.IPv4(bytes[offset:offset+header.IPv4MaximumHeaderSize])
hdr.SetDestinationAddressWithChecksumUpdate(f.sourceIp)
//packetInfo(bytes[offset:])
return i, nil
}
func packetInfo(bytes []byte) {
log.Printf(
"proto %d checksum %x from %s to %s\n",
bytes[9], bytes[10:12],
net.IPv4(bytes[12], bytes[13], bytes[14], bytes[15]),
net.IPv4(bytes[16], bytes[17], bytes[18], bytes[19]),
)
}
func (f *FakeTun) Write(bytes []byte, offset int) (int, error) {
//log.Println("Write")
bytes = bytes[offset:]
hdr := header.IPv4(bytes[:header.IPv4MinimumSize])
// nobody uses options anyway ...right?
f.sourceIp = hdr.SourceAddress()
//packetInfo(bytes)
//log.Println(hdr.Protocol(), hdr.Checksum(), hdr.IsChecksumValid(), hdr.SourceAddress(), hdr.DestinationAddress())
hdr.SetSourceAddressWithChecksumUpdate(f.clientIp)
// fix tcp checksum
if hdr.Protocol() == uint8(header.TCPProtocolNumber) {
hdrTcp := header.TCP(bytes[header.IPv4MinimumSize:header.IPv4MinimumSize+header.TCPMinimumSize])
hdrTcp.UpdateChecksumPseudoHeaderAddress(f.sourceIp, f.clientIp, true)
}
//packetInfo(bytes)
//log.Println(hdr.Protocol(), hdr.Checksum(), hdr.IsChecksumValid(), hdr.SourceAddress(), hdr.DestinationAddress())
//fmt.Println(hex.Dump(bytes))
return f.file.Write(bytes)
}
func (f *FakeTun) Flush() error {
return nil
}
func (f *FakeTun) MTU() (int, error) {
return device.DefaultMTU, nil
}
func (f *FakeTun) Name() (string, error) {
return "", nil
}
func (f *FakeTun) Events() chan tun.Event {
ch := make(chan tun.Event)
return ch
}
func (f *FakeTun) Close() error {
return f.file.Close()
}