diff --git a/proxy/config.go b/proxy/config.go index 6e32ac1..34e0f7f 100644 --- a/proxy/config.go +++ b/proxy/config.go @@ -56,10 +56,13 @@ type Proxy struct { } type Stratum struct { - Enabled bool `json:"enabled"` - Listen string `json:"listen"` - Timeout string `json:"timeout"` - MaxConn int `json:"maxConn"` + Enabled bool `json:"enabled"` + Listen string `json:"listen"` + Timeout string `json:"timeout"` + MaxConn int `json:"maxConn"` + TLS bool `json:"tls` + CertFile string `json:"certFile` + KeyFile string `json:"keyFile` } type Upstream struct { diff --git a/proxy/proxy.go b/proxy/proxy.go index 276c8ae..a012e3a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -34,13 +34,6 @@ type ProxyServer struct { sessionsMu sync.RWMutex sessions map[*Session]struct{} timeout time.Duration - Extranonce string -} - -type jobDetails struct { - JobID string - SeedHash string - HeaderHash string } type Session struct { @@ -49,11 +42,9 @@ type Session struct { // Stratum sync.Mutex - conn *net.TCPConn + conn net.Conn login string lastErr error - subscriptionID string - JobDeatils jobDetails } func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer { diff --git a/proxy/stratum.go b/proxy/stratum.go index 50422ba..7df8789 100644 --- a/proxy/stratum.go +++ b/proxy/stratum.go @@ -2,6 +2,7 @@ package proxy import ( "bufio" + "crypto/tls" "encoding/json" "errors" "io" @@ -17,14 +18,26 @@ const ( ) func (s *ProxyServer) ListenTCP() { - timeout := util.MustParseDuration(s.config.Proxy.Stratum.Timeout) - s.timeout = timeout - - addr, err := net.ResolveTCPAddr("tcp4", s.config.Proxy.Stratum.Listen) - if err != nil { - log.Fatalf("Error: %v", err) + s.timeout = util.MustParseDuration(s.config.Proxy.Stratum.Timeout) + + var err error + var server net.Listener + setKeepAlive := func(net.Conn) {} + if s.config.Proxy.Stratum.TLS { + var cert tls.Certificate + cert, err = tls.LoadX509KeyPair(s.config.Proxy.Stratum.CertFile, s.config.Proxy.Stratum.KeyFile) + if err != nil { + log.Fatalln("Error loading certificate:", err) + } + tlsCfg := &tls.Config{Certificates: []tls.Certificate{cert}} + server, err = tls.Listen("tcp4", s.config.Proxy.Stratum.Listen, tlsCfg) + } else { + server, err = net.Listen("tcp4", s.config.Proxy.Stratum.Listen) + setKeepAlive = func(conn net.Conn) { + conn.(*net.TCPConn).SetKeepAlive(true) + } } - server, err := net.ListenTCP("tcp4", addr) + if err != nil { log.Fatalf("Error: %v", err) } @@ -35,11 +48,11 @@ func (s *ProxyServer) ListenTCP() { n := 0 for { - conn, err := server.AcceptTCP() + conn, err := server.Accept() if err != nil { continue } - conn.SetKeepAlive(true) + setKeepAlive(conn) ip, _, _ := net.SplitHostPort(conn.RemoteAddr().String()) @@ -168,7 +181,7 @@ func (cs *Session) sendTCPError(id json.RawMessage, reply *ErrorReply) error { return errors.New(reply.Message) } -func (self *ProxyServer) setDeadline(conn *net.TCPConn) { +func (self *ProxyServer) setDeadline(conn net.Conn) { conn.SetDeadline(time.Now().Add(self.timeout)) }