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

Add support for TLS to stratum without breaking standard stratum #427

Open
wants to merge 3 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ otherwise you will get errors on start because of JSON comments.**
// Bind stratum mining socket to this IP:PORT
"listen": "0.0.0.0:8008",
"timeout": "120s",
"maxConn": 8192
"maxConn": 8192,
"tls": false,
"certFile": "/path/to/cert.pem",
"keyFile": "/path/to/key.pem"
},

// Try to get new job from geth in this interval
Expand Down
5 changes: 4 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"enabled": true,
"listen": "0.0.0.0:8008",
"timeout": "120s",
"maxConn": 8192
"maxConn": 8192,
"tls": false,
"certFile": "/path/to/cert.pem",
"keyFile": "/path/to/key.pem"
},

"policy": {
Expand Down
11 changes: 7 additions & 4 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,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 {
Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Session struct {

// Stratum
sync.Mutex
conn *net.TCPConn
conn net.Conn
login string
}

Expand Down
32 changes: 22 additions & 10 deletions proxy/stratum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxy

import (
"bufio"
"crypto/tls"
"encoding/json"
"errors"
"io"
Expand All @@ -17,14 +18,25 @@ const (
)

func (s *ProxyServer) ListenTCP() {
timeout := util.MustParseDuration(s.config.Proxy.Stratum.Timeout)
s.timeout = timeout

addr, err := net.ResolveTCPAddr("tcp", 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("tcp", s.config.Proxy.Stratum.Listen, tlsCfg)
} else {
server, err = net.Listen("tcp", s.config.Proxy.Stratum.Listen)
setKeepAlive = func(conn net.Conn) {
conn.(*net.TCPConn).SetKeepAlive(true)
}
}
server, err := net.ListenTCP("tcp", addr)
if err != nil {
log.Fatalf("Error: %v", err)
}
Expand All @@ -35,11 +47,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())

Expand Down Expand Up @@ -169,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))
}

Expand Down