fea: tls for socks5
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled

This commit is contained in:
Daniel Ding
2025-03-22 10:18:20 +08:00
parent 269ed244c0
commit 2f2e20e46a
13 changed files with 53 additions and 32 deletions

View File

@@ -2,7 +2,7 @@ package api
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io"
"net/http" "net/http"
"github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/libol"
@@ -40,7 +40,7 @@ func ResponseYaml(w http.ResponseWriter, v interface{}) {
} }
func GetData(r *http.Request, v interface{}) error { func GetData(r *http.Request, v interface{}) error {
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return err return err
} }

4
pkg/cache/user.go vendored
View File

@@ -4,7 +4,7 @@ import (
"bufio" "bufio"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io/ioutil" "os"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -174,7 +174,7 @@ func (w *user) Timeout(user *models.User) bool {
func (w *user) Check(obj *models.User) (*models.User, error) { func (w *user) Check(obj *models.User) (*models.User, error) {
if w.Cert != "" { if w.Cert != "" {
pemData, err := ioutil.ReadFile(w.Cert) pemData, err := os.ReadFile(w.Cert)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -4,8 +4,9 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"os"
"github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/libol"
"io/ioutil"
) )
type Crypt struct { type Crypt struct {
@@ -67,7 +68,7 @@ func (c *Cert) GetCertPool() *x509.CertPool {
libol.Debug("Cert.GetTlsCertPool: %s not such file", c.CaFile) libol.Debug("Cert.GetTlsCertPool: %s not such file", c.CaFile)
return nil return nil
} }
caCert, err := ioutil.ReadFile(c.CaFile) caCert, err := os.ReadFile(c.CaFile)
if err != nil { if err != nil {
libol.Warn("Cert.GetTlsCertPool: %s", err) libol.Warn("Cert.GetTlsCertPool: %s", err)
return nil return nil

View File

@@ -83,6 +83,7 @@ type SocksProxy struct {
Listen string `json:"listen,omitempty" yaml:"listen,omitempty"` Listen string `json:"listen,omitempty" yaml:"listen,omitempty"`
Auth *Password `json:"auth,omitempty" yaml:"auth,omitempty"` Auth *Password `json:"auth,omitempty" yaml:"auth,omitempty"`
Backends HttpBackends `json:"backends,omitempty" yaml:"backends,omitempty"` Backends HttpBackends `json:"backends,omitempty" yaml:"backends,omitempty"`
Cert *Cert `json:"cert,omitempty" yaml:"cert,omitempty"`
} }
func (s *SocksProxy) Initialize() error { func (s *SocksProxy) Initialize() error {

View File

@@ -3,9 +3,9 @@ package libol
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"os"
"time" "time"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
@@ -162,7 +162,7 @@ func NewWebClientFromConn(conn net.Conn, cfg *WebConfig) *WebClient {
} }
func (t *WebClient) GetCertPool(ca string) *x509.CertPool { func (t *WebClient) GetCertPool(ca string) *x509.CertPool {
caCert, err := ioutil.ReadFile(ca) caCert, err := os.ReadFile(ca)
if err != nil { if err != nil {
Error("WebClient.GetCertPool: %s", err) Error("WebClient.GetCertPool: %s", err)
return nil return nil

View File

@@ -458,7 +458,6 @@ func (t *HttpProxy) Start() {
return err return err
} }
} }
t.server.Shutdown(nil)
return nil return nil
}) })
} }

View File

@@ -1,6 +1,7 @@
package proxy package proxy
import ( import (
"crypto/tls"
"time" "time"
"github.com/luscis/openlan/pkg/config" "github.com/luscis/openlan/pkg/config"
@@ -36,6 +37,12 @@ func NewSocksProxy(cfg *config.SocksProxy) *SocksProxy {
AuthMethods: authMethods, AuthMethods: authMethods,
Logger: s.out, Logger: s.out,
} }
crt := cfg.Cert
if crt != nil && crt.KeyFile != "" {
conf.TlsConfig = &tls.Config{
RootCAs: crt.GetCertPool(),
}
}
server, err := socks5.New(conf) server, err := socks5.New(conf)
if err != nil { if err != nil {
s.out.Error("NewSocksProxy %s", err) s.out.Error("NewSocksProxy %s", err)
@@ -50,7 +57,13 @@ func (s *SocksProxy) Start() {
return return
} }
addr := s.cfg.Listen addr := s.cfg.Listen
crt := s.cfg.Cert
if crt == nil || crt.KeyFile == "" {
s.out.Info("SocksProxy.Start: socks5://%s", s.cfg.Listen) s.out.Info("SocksProxy.Start: socks5://%s", s.cfg.Listen)
} else {
s.out.Info("SocksProxy.Start: sockss://%s", s.cfg.Listen)
}
promise := &libol.Promise{ promise := &libol.Promise{
First: time.Second * 2, First: time.Second * 2,

View File

@@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"errors" "errors"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
"sync" "sync"
@@ -120,7 +119,7 @@ func tcpRemote(addr string, shadow func(net.Conn) net.Conn) {
logf("failed to get target address from %v: %v", c.RemoteAddr(), err) logf("failed to get target address from %v: %v", c.RemoteAddr(), err)
// drain c to avoid leaking server behavioral features // drain c to avoid leaking server behavioral features
// see https://www.ndss-symposium.org/ndss-paper/detecting-probe-resistant-proxies/ // see https://www.ndss-symposium.org/ndss-paper/detecting-probe-resistant-proxies/
_, err = io.Copy(ioutil.Discard, c) _, err = io.Copy(io.Discard, c)
if err != nil { if err != nil {
logf("discard error: %v", err) logf("discard error: %v", err)
} }

View File

@@ -2,6 +2,7 @@ package socks5
import ( import (
"bufio" "bufio"
"crypto/tls"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
@@ -54,6 +55,9 @@ type Config struct {
// Backends forwarding socks request // Backends forwarding socks request
Backends co.FindBackend Backends co.FindBackend
// TLS Configurations
TlsConfig *tls.Config
} }
// Server is reponsible for accepting connections and handling // Server is reponsible for accepting connections and handling
@@ -104,10 +108,18 @@ func New(conf *Config) (*Server, error) {
// ListenAndServe is used to create a listener and serve on it // ListenAndServe is used to create a listener and serve on it
func (s *Server) ListenAndServe(network, addr string) error { func (s *Server) ListenAndServe(network, addr string) error {
l, err := net.Listen(network, addr) var l net.Listener
var err error
if s.config.TlsConfig != nil {
l, err = tls.Listen(network, addr, s.config.TlsConfig)
} else {
l, err = net.Listen(network, addr)
}
if err != nil { if err != nil {
return err return err
} }
return s.Serve(l) return s.Serve(l)
} }

View File

@@ -2,7 +2,6 @@ package cswitch
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -85,7 +84,7 @@ func (d *Dhcp) SaveConf() {
cfg.Subnet.End, cfg.Subnet.End,
d.LeaseFile(), d.LeaseFile(),
) )
_ = ioutil.WriteFile(d.ConfFile(), []byte(data), 0600) _ = os.WriteFile(d.ConfFile(), []byte(data), 0600)
} }
func (d *Dhcp) Start() { func (d *Dhcp) Start() {
@@ -124,7 +123,7 @@ func (d *Dhcp) Clean() {
} }
func (d *Dhcp) Stop() { func (d *Dhcp) Stop() {
if data, err := ioutil.ReadFile(d.PidFile()); err != nil { if data, err := os.ReadFile(d.PidFile()); err != nil {
d.out.Info("Dhcp.Stop %s", err) d.out.Info("Dhcp.Stop %s", err)
} else { } else {
pid := strings.TrimSpace(string(data)) pid := strings.TrimSpace(string(data))

View File

@@ -3,7 +3,6 @@ package cswitch
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"os" "os"
@@ -145,7 +144,7 @@ func (h *Http) LoadToken() {
if _, err := os.Stat(h.adminFile); os.IsNotExist(err) { if _, err := os.Stat(h.adminFile); os.IsNotExist(err) {
libol.Info("Http.LoadToken: file:%s does not exist", h.adminFile) libol.Info("Http.LoadToken: file:%s does not exist", h.adminFile)
} else { } else {
contents, err := ioutil.ReadFile(h.adminFile) contents, err := os.ReadFile(h.adminFile)
if err != nil { if err != nil {
libol.Error("Http.LoadToken: file:%s %s", h.adminFile, err) libol.Error("Http.LoadToken: file:%s %s", h.adminFile, err)
} else { } else {
@@ -235,7 +234,7 @@ func (h *Http) getFile(name string) string {
func (h *Http) PubFile(w http.ResponseWriter, r *http.Request) { func (h *Http) PubFile(w http.ResponseWriter, r *http.Request) {
realpath := h.getFile(r.URL.Path) realpath := h.getFile(r.URL.Path)
contents, err := ioutil.ReadFile(realpath) contents, err := os.ReadFile(realpath)
if err != nil { if err != nil {
_, _ = fmt.Fprintf(w, "404") _, _ = fmt.Fprintf(w, "404")
return return

View File

@@ -1,7 +1,6 @@
package cswitch package cswitch
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -117,7 +116,7 @@ func (l *Link) Clean() {
} }
func (l *Link) Stop() error { func (l *Link) Stop() error {
if data, err := ioutil.ReadFile(l.PidFile()); err != nil { if data, err := os.ReadFile(l.PidFile()); err != nil {
l.out.Debug("Link.Stop %s", err) l.out.Debug("Link.Stop %s", err)
} else { } else {
pid := strings.TrimSpace(string(data)) pid := strings.TrimSpace(string(data))

View File

@@ -3,7 +3,6 @@ package cswitch
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@@ -278,7 +277,7 @@ func (o *OpenVPN) ServerTmpl() string {
tmplStr = certConfTmpl tmplStr = certConfTmpl
} }
cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"server.tmpl") cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"server.tmpl")
_ = ioutil.WriteFile(cfgTmpl, []byte(tmplStr), 0600) _ = os.WriteFile(cfgTmpl, []byte(tmplStr), 0600)
return tmplStr return tmplStr
} }
@@ -286,7 +285,7 @@ func (o *OpenVPN) ClientConnectScriptTmpl() string {
tmplStr := clientConnectScriptTmpl tmplStr := clientConnectScriptTmpl
cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"connectivplat.tmpl") cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"connectivplat.tmpl")
_ = ioutil.WriteFile(cfgTmpl, []byte(tmplStr), 0600) _ = os.WriteFile(cfgTmpl, []byte(tmplStr), 0600)
return tmplStr return tmplStr
} }
@@ -294,7 +293,7 @@ func (o *OpenVPN) ClientDisConnectScriptTmpl() string {
tmplStr := clientDisConnectScriptTmpl tmplStr := clientDisConnectScriptTmpl
cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"disconnectivplat.tmpl") cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"disconnectivplat.tmpl")
_ = ioutil.WriteFile(cfgTmpl, []byte(tmplStr), 0600) _ = os.WriteFile(cfgTmpl, []byte(tmplStr), 0600)
return tmplStr return tmplStr
} }
@@ -310,7 +309,7 @@ func (o *OpenVPN) FileIpp(full bool) string {
} }
func (o *OpenVPN) Pid(full bool) string { func (o *OpenVPN) Pid(full bool) string {
if data, err := ioutil.ReadFile(o.FilePid(true)); err != nil { if data, err := os.ReadFile(o.FilePid(true)); err != nil {
o.out.Debug("OpenVPN.Stop %s", err) o.out.Debug("OpenVPN.Stop %s", err)
return "" return ""
} else { } else {
@@ -369,7 +368,7 @@ func (o *OpenVPN) writeClientConfig() error {
} }
ficFile := filepath.Join(ccd, fic.Name) ficFile := filepath.Join(ccd, fic.Name)
pushIP := fmt.Sprintf("ifconfig-push %s %s", fic.Address, fic.Netmask) pushIP := fmt.Sprintf("ifconfig-push %s %s", fic.Address, fic.Netmask)
if err := ioutil.WriteFile(ficFile, []byte(pushIP), 0600); err != nil { if err := os.WriteFile(ficFile, []byte(pushIP), 0600); err != nil {
o.out.Warn("OpenVPN.writeClientConfig %s", err) o.out.Warn("OpenVPN.writeClientConfig %s", err)
} }
} }
@@ -464,7 +463,7 @@ func (o *OpenVPN) Initialize() {
} }
if ctx, err := o.Profile(); err == nil { if ctx, err := o.Profile(); err == nil {
file := o.FileClient(true) file := o.FileClient(true)
if err := ioutil.WriteFile(file, ctx, 0600); err != nil { if err := os.WriteFile(file, ctx, 0600); err != nil {
o.out.Warn("OpenVPN.Initialize %s", err) o.out.Warn("OpenVPN.Initialize %s", err)
} }
} else { } else {
@@ -511,7 +510,7 @@ func (o *OpenVPN) Stop() {
if !o.ValidConf() { if !o.ValidConf() {
return return
} }
if data, err := ioutil.ReadFile(o.FilePid(true)); err != nil { if data, err := os.ReadFile(o.FilePid(true)); err != nil {
o.out.Debug("OpenVPN.Stop %s", err) o.out.Debug("OpenVPN.Stop %s", err)
} else { } else {
killPath, err := exec.LookPath("kill") killPath, err := exec.LookPath("kill")
@@ -569,7 +568,7 @@ func (o *OpenVPN) ProfileTmpl() string {
} }
cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"client.tmpl") cfgTmpl := filepath.Join(o.Cfg.Directory, o.ID()+"client.tmpl")
_ = ioutil.WriteFile(cfgTmpl, []byte(tmplStr), 0600) _ = os.WriteFile(cfgTmpl, []byte(tmplStr), 0600)
return tmplStr return tmplStr
} }
@@ -667,10 +666,10 @@ func NewOpenVPNProfileFromConf(obj *OpenVPN) *OpenVPNProfile {
data.Server = name data.Server = name
} }
} }
if ctx, err := ioutil.ReadFile(cfg.RootCa); err == nil { if ctx, err := os.ReadFile(cfg.RootCa); err == nil {
data.Ca = string(ctx) data.Ca = string(ctx)
} }
if ctx, err := ioutil.ReadFile(cfg.TlsAuth); err == nil { if ctx, err := os.ReadFile(cfg.TlsAuth); err == nil {
data.TlsAuth = string(ctx) data.TlsAuth = string(ctx)
} }
return data return data