Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for HTTPS #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ BindAddress = 127.0.0.1:25345
#Username = ...
# Avoid using spaces in the password field
#Password = ...

# Specifying certificate and key enables HTTPS
#CertFile = ...
#KeyFile = ...
```

Alternatively, if you already have a wireguard config, you can import it in the
Expand Down
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type HTTPConfig struct {
BindAddress string
Username string
Password string
CertFile string
KeyFile string
}

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

certFile, _ := parseString(section, "CertFile")
config.CertFile = certFile

keyFile, _ := parseString(section, "KeyFile")
config.KeyFile = keyFile

return config, nil
}

Expand Down
17 changes: 16 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
Expand All @@ -23,6 +24,7 @@
dial func(network, address string) (net.Conn, error)

authRequired bool
tlsRequired bool
}

func (s *HTTPServer) authenticate(req *http.Request) (int, error) {
Expand All @@ -47,7 +49,7 @@
return http.StatusUnauthorized, fmt.Errorf("username and password not matching")
}

return http.StatusProxyAuthRequired, fmt.Errorf(http.StatusText(http.StatusProxyAuthRequired))

Check failure on line 52 in http.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Errorf (govet)
}

func (s *HTTPServer) handleConn(req *http.Request, conn net.Conn) (peer net.Conn, err error) {
Expand Down Expand Up @@ -141,9 +143,22 @@
}()
}

func (s *HTTPServer) listen(network, addr string) (net.Listener, error) {
if s.tlsRequired {
cert, err := tls.LoadX509KeyPair(s.config.CertFile, s.config.KeyFile)
if err != nil {
return nil, err
}

return tls.Listen(network, addr, &tls.Config{Certificates: []tls.Certificate{cert}})
}

return net.Listen(network, addr)
}

// ListenAndServe is used to create a listener and serve on it
func (s *HTTPServer) ListenAndServe(network, addr string) error {
server, err := net.Listen(network, addr)
server, err := s.listen(network, addr)
if err != nil {
return fmt.Errorf("listen tcp failed: %w", err)
}
Expand Down
16 changes: 0 additions & 16 deletions net.go

This file was deleted.

10 changes: 7 additions & 3 deletions routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (config *HTTPConfig) SpawnRoutine(vt *VirtualTun) {
server.authRequired = true
}

if config.CertFile != "" && config.KeyFile != "" {
server.tlsRequired = true
}

if err := server.ListenAndServe("tcp", config.BindAddress); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -202,7 +206,7 @@ func tcpClientForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
return
}

tcpAddr := TCPAddrFromAddrPort(*target)
tcpAddr := net.TCPAddrFromAddrPort(*target)

sconn, err := vt.Tnet.DialTCP(tcpAddr)
if err != nil {
Expand Down Expand Up @@ -241,7 +245,7 @@ func STDIOTcpForward(vt *VirtualTun, raddr *addressPort) {
return
}

tcpAddr := TCPAddrFromAddrPort(*target)
tcpAddr := net.TCPAddrFromAddrPort(*target)
sconn, err := vt.Tnet.DialTCP(tcpAddr)
if err != nil {
errorLogger.Printf("TCP Client Tunnel to %s (%s): %s\n", target, tcpAddr, err.Error())
Expand Down Expand Up @@ -301,7 +305,7 @@ func tcpServerForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
return
}

tcpAddr := TCPAddrFromAddrPort(*target)
tcpAddr := net.TCPAddrFromAddrPort(*target)

sconn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
Expand Down
Loading