Skip to content

Commit a21bd62

Browse files
committed
Add support for HTTPS
1 parent cb1f39b commit a21bd62

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ BindAddress = 127.0.0.1:25345
131131
#Username = ...
132132
# Avoid using spaces in the password field
133133
#Password = ...
134+
135+
# Specifying certificate and key enables HTTPS
136+
#CertFile = ...
137+
#KeyFile = ...
134138
```
135139

136140
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
@@ -57,6 +57,8 @@ type HTTPConfig struct {
5757
BindAddress string
5858
Username string
5959
Password string
60+
CertFile string
61+
KeyFile string
6062
}
6163

6264
type Configuration struct {
@@ -431,6 +433,12 @@ func parseHTTPConfig(section *ini.Section) (RoutineSpawner, error) {
431433
password, _ := parseString(section, "Password")
432434
config.Password = password
433435

436+
certFile, _ := parseString(section, "CertFile")
437+
config.CertFile = certFile
438+
439+
keyFile, _ := parseString(section, "KeyFile")
440+
config.KeyFile = keyFile
441+
434442
return config, nil
435443
}
436444

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"
@@ -23,6 +24,7 @@ type HTTPServer struct {
2324
dial func(network, address string) (net.Conn, error)
2425

2526
authRequired bool
27+
tlsRequired bool
2628
}
2729

2830
func (s *HTTPServer) authenticate(req *http.Request) (int, error) {
@@ -141,9 +143,22 @@ func (s *HTTPServer) serve(conn net.Conn) {
141143
}()
142144
}
143145

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

routine.go

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

176+
if config.CertFile != "" && config.KeyFile != "" {
177+
server.tlsRequired = true
178+
}
179+
176180
if err := server.ListenAndServe("tcp", config.BindAddress); err != nil {
177181
log.Fatal(err)
178182
}

0 commit comments

Comments
 (0)