-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwfwd.go
More file actions
153 lines (129 loc) · 3.87 KB
/
wfwd.go
File metadata and controls
153 lines (129 loc) · 3.87 KB
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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"net/netip"
"os"
"strconv"
"strings"
"golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/tun/netstack"
)
func forward(localConn net.Conn, tunnelPortAddress string) {
tunnel, err := net.Dial("tcp", tunnelPortAddress)
if err != nil {
fmt.Printf("ssh.Dial failed: %s", err)
}
go func() {
_, err = io.Copy(tunnel, localConn)
if err != nil {
fmt.Printf("io.Copy failed: %v", err)
}
}()
go func() {
_, err = io.Copy(localConn, tunnel)
if err != nil {
fmt.Printf("io.Copy failed: %v", err)
}
}()
}
func tcpForwarder(tunnelAddress string, ipToIntercept string, portToIntercept string, wgListenPort string, wgPrivateKey string, wgPublicKey string, wgAllowedIp string) error {
mtu := 1420
localAddresses := []netip.Addr{netip.MustParseAddr(ipToIntercept)}
dnsServers := []netip.Addr{}
tun, tnet, err := netstack.CreateNetTUN(
localAddresses,
dnsServers,
mtu)
if err != nil {
fmt.Printf("Error creating tunnel: %s", err)
return err
}
dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, ""))
dev.IpcSet("private_key=" + wgPrivateKey + "\n" +
"listen_port=" + wgListenPort + "\n" +
"public_key=" + wgPublicKey + "\n" +
"allowed_ip=" + wgAllowedIp)
interceptPort, err := strconv.Atoi(portToIntercept)
if err != nil {
fmt.Printf("Error parsing portToIntercept value %s: %s", portToIntercept, err)
return err
}
// user land net stack for pulling out the traffic we want to forward
localListener, err := tnet.ListenTCP(&net.TCPAddr{Port: interceptPort})
if err != nil {
fmt.Printf("net.Listen failed: %v", err)
return err
}
for {
localConn, err := localListener.Accept()
if err != nil {
fmt.Printf("listen.Accept failed: %v", err)
}
go forward(localConn, tunnelAddress)
}
}
func Runfwd(tunnelAddress string, ipToIntercept string, portToIntercept string, wgListenPort string, wgPrivateKey string, wgPublicKey string, wgAllowedIp string) error {
err := tcpForwarder(tunnelAddress, ipToIntercept, portToIntercept, wgListenPort, wgPrivateKey, wgPublicKey, wgAllowedIp)
return err
}
// go build wfwd.go
// go run wfwd.go
func main() {
if len(os.Args) != 3 {
fmt.Println("Wireguard Forwarder: wfwd [tunnel address and port]] [config file]")
fmt.Println("example: wfwd localhost:61704 exampleconfig.conf")
} else {
tunnelAddress := os.Args[1]
configPath := os.Args[2]
// IP address we intercept and forward e.g., 10.0.0.1
ipToIntercept := ""
// Port we are intercepting e.g., 80
portToIntercept := ""
// The port we receive wg traffic on e.g., 55211
wgListenPort := ""
// Our Secret Key (hex encoded)
wgPrivateKey := ""
// Pubkey of wg client sending us traffic (hex encoded)
wgPublicKey := ""
// IP range we say we route e.g., 10.0.0.1/8
wgAllowedIp := ""
var configfile, err = os.OpenFile(configPath, os.O_RDWR, 0644)
if err != nil {
fmt.Printf("Error opening config file %s, %s", configPath, err)
return
}
defer configfile.Close()
scanner := bufio.NewScanner(configfile)
// optionally, resize scanner's capacity for lines over 64K, see next example
for scanner.Scan() {
line := scanner.Text()
if line[0:2] == "//" {
continue
}
key := strings.Split(line, "=")[0]
value := strings.Split(line, "=")[1]
if key == "ipToIntercept" {
ipToIntercept = value
} else if key == "portToIntercept" {
portToIntercept = value
} else if key == "wgListenPort" {
wgListenPort = value
} else if key == "wgPrivateKey" {
wgPrivateKey = value
} else if key == "wgPublicKey" {
wgPublicKey = value
} else if key == "wgAllowedIp" {
wgAllowedIp = value
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
Runfwd(tunnelAddress, ipToIntercept, portToIntercept, wgListenPort, wgPrivateKey, wgPublicKey, wgAllowedIp)
}
}