Browse Source

crypto/tls

master
yuriy0803 3 years ago
parent
commit
a9873001f7
  1. 11
      proxy/config.go
  2. 11
      proxy/proxy.go
  3. 33
      proxy/stratum.go

11
proxy/config.go

@ -56,10 +56,13 @@ type Proxy struct {
} }
type Stratum struct { type Stratum struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
Listen string `json:"listen"` Listen string `json:"listen"`
Timeout string `json:"timeout"` Timeout string `json:"timeout"`
MaxConn int `json:"maxConn"` MaxConn int `json:"maxConn"`
TLS bool `json:"tls`
CertFile string `json:"certFile`
KeyFile string `json:"keyFile`
} }
type Upstream struct { type Upstream struct {

11
proxy/proxy.go

@ -34,13 +34,6 @@ type ProxyServer struct {
sessionsMu sync.RWMutex sessionsMu sync.RWMutex
sessions map[*Session]struct{} sessions map[*Session]struct{}
timeout time.Duration timeout time.Duration
Extranonce string
}
type jobDetails struct {
JobID string
SeedHash string
HeaderHash string
} }
type Session struct { type Session struct {
@ -49,11 +42,9 @@ type Session struct {
// Stratum // Stratum
sync.Mutex sync.Mutex
conn *net.TCPConn conn net.Conn
login string login string
lastErr error lastErr error
subscriptionID string
JobDeatils jobDetails
} }
func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer { func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer {

33
proxy/stratum.go

@ -2,6 +2,7 @@ package proxy
import ( import (
"bufio" "bufio"
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
@ -17,14 +18,26 @@ const (
) )
func (s *ProxyServer) ListenTCP() { func (s *ProxyServer) ListenTCP() {
timeout := util.MustParseDuration(s.config.Proxy.Stratum.Timeout) s.timeout = util.MustParseDuration(s.config.Proxy.Stratum.Timeout)
s.timeout = timeout
var err error
addr, err := net.ResolveTCPAddr("tcp4", s.config.Proxy.Stratum.Listen) var server net.Listener
if err != nil { setKeepAlive := func(net.Conn) {}
log.Fatalf("Error: %v", err) 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 { if err != nil {
log.Fatalf("Error: %v", err) log.Fatalf("Error: %v", err)
} }
@ -35,11 +48,11 @@ func (s *ProxyServer) ListenTCP() {
n := 0 n := 0
for { for {
conn, err := server.AcceptTCP() conn, err := server.Accept()
if err != nil { if err != nil {
continue continue
} }
conn.SetKeepAlive(true) setKeepAlive(conn)
ip, _, _ := net.SplitHostPort(conn.RemoteAddr().String()) 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) 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)) conn.SetDeadline(time.Now().Add(self.timeout))
} }

Loading…
Cancel
Save