Skip to content

Commit 04030e8

Browse files
dima424658windtf
andauthored
Support for HTTPS (#133)
* Add support for HTTPS * Replace TCPAddrFromAddrPort with built-in one * Fix TCPAddr function reference in routine.go --------- Co-authored-by: Wind Wong <im@windtfw.com>
1 parent e76e370 commit 04030e8

5 files changed

Lines changed: 35 additions & 20 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ BindAddress = 127.0.0.1:25345
152152
#Username = ...
153153
# Avoid using spaces in the password field
154154
#Password = ...
155+
156+
# Specifying certificate and key enables HTTPS
157+
#CertFile = ...
158+
#KeyFile = ...
155159
```
156160

157161
Alternatively, if you already have a wireguard config, you can import it in the

config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type HTTPConfig struct {
6565
BindAddress string
6666
Username string
6767
Password string
68+
CertFile string
69+
KeyFile string
6870
}
6971

7072
type Configuration struct {
@@ -442,6 +444,12 @@ func parseHTTPConfig(section *ini.Section) (RoutineSpawner, error) {
442444
password, _ := parseString(section, "Password")
443445
config.Password = password
444446

447+
certFile, _ := parseString(section, "CertFile")
448+
config.CertFile = certFile
449+
450+
keyFile, _ := parseString(section, "KeyFile")
451+
config.KeyFile = keyFile
452+
445453
return config, nil
446454
}
447455

http.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package wireproxy
33
import (
44
"bufio"
55
"bytes"
6+
"crypto/tls"
67
"encoding/base64"
78
"fmt"
89
"io"
@@ -21,6 +22,7 @@ type HTTPServer struct {
2122
dial func(network, address string) (net.Conn, error)
2223

2324
authRequired bool
25+
tlsRequired bool
2426
}
2527

2628
func (s *HTTPServer) authenticate(req *http.Request) (int, error) {
@@ -145,9 +147,22 @@ func (s *HTTPServer) serve(conn net.Conn) {
145147
}()
146148
}
147149

150+
func (s *HTTPServer) listen(network, addr string) (net.Listener, error) {
151+
if s.tlsRequired {
152+
cert, err := tls.LoadX509KeyPair(s.config.CertFile, s.config.KeyFile)
153+
if err != nil {
154+
return nil, err
155+
}
156+
157+
return tls.Listen(network, addr, &tls.Config{Certificates: []tls.Certificate{cert}})
158+
}
159+
160+
return net.Listen(network, addr)
161+
}
162+
148163
// ListenAndServe is used to create a listener and serve on it
149164
func (s *HTTPServer) ListenAndServe(network, addr string) error {
150-
server, err := net.Listen(network, addr)
165+
server, err := s.listen(network, addr)
151166
if err != nil {
152167
return fmt.Errorf("listen tcp failed: %w", err)
153168
}

net.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

routine.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func (config *HTTPConfig) SpawnRoutine(vt *VirtualTun) {
174174
server.authRequired = true
175175
}
176176

177+
if config.CertFile != "" && config.KeyFile != "" {
178+
server.tlsRequired = true
179+
}
180+
177181
if err := server.ListenAndServe("tcp", config.BindAddress); err != nil {
178182
log.Fatal(err)
179183
}
@@ -206,7 +210,7 @@ func tcpClientForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
206210
return
207211
}
208212

209-
tcpAddr := TCPAddrFromAddrPort(*target)
213+
tcpAddr := net.TCPAddrFromAddrPort(*target)
210214

211215
sconn, err := vt.Tnet.DialTCP(tcpAddr)
212216
if err != nil {
@@ -226,7 +230,7 @@ func STDIOTcpForward(vt *VirtualTun, raddr *addressPort, input *os.File, output
226230
return
227231
}
228232

229-
tcpAddr := TCPAddrFromAddrPort(*target)
233+
tcpAddr := net.TCPAddrFromAddrPort(*target)
230234
sconn, err := vt.Tnet.DialTCP(tcpAddr)
231235
if err != nil {
232236
errorLogger.Printf("TCP Client Tunnel to %s (%s): %s\n", target, tcpAddr, err.Error())
@@ -276,7 +280,7 @@ func tcpServerForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
276280
return
277281
}
278282

279-
tcpAddr := TCPAddrFromAddrPort(*target)
283+
tcpAddr := net.TCPAddrFromAddrPort(*target)
280284

281285
sconn, err := net.DialTCP("tcp", nil, tcpAddr)
282286
if err != nil {

0 commit comments

Comments
 (0)