diff --git a/server/go.mod b/server/go.mod index 8f3a1af..141c4aa 100644 --- a/server/go.mod +++ b/server/go.mod @@ -3,10 +3,15 @@ module github.com/swordlet/xmrig2xdag go 1.17 require ( + github.com/didip/tollbooth v4.0.2+incompatible github.com/kelseyhightower/envconfig v1.4.0 github.com/pkg/errors v0.9.1 github.com/powerman/rpc-codec v1.2.2 github.com/sourcegraph/jsonrpc2 v0.1.0 golang.org/x/net v0.0.0-20220225172249-27dd8689420f - golang.org/x/time v0.3.0 +) + +require ( + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect + golang.org/x/time v0.3.0 // indirect ) diff --git a/server/go.sum b/server/go.sum index 4395247..efd0203 100644 --- a/server/go.sum +++ b/server/go.sum @@ -1,7 +1,11 @@ +github.com/didip/tollbooth v4.0.2+incompatible h1:fVSa33JzSz0hoh2NxpwZtksAzAgd7zjmGO20HCZtF4M= +github.com/didip/tollbooth v4.0.2+incompatible/go.mod h1:A9b0665CE6l1KmzpDws2++elm/CsuWBMa5Jv4WY0PEY= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/powerman/rpc-codec v1.2.2 h1:BK0JScZivljhwW/vLLhZLtUgqSxc/CD3sHEs8LiwwKw= diff --git a/server/main.go b/server/main.go index 117a9af..0be69dc 100644 --- a/server/main.go +++ b/server/main.go @@ -14,7 +14,7 @@ import ( ) var ( - version = "2.0.1" + version = "2.0.2" // cmd line options configFile *string diff --git a/server/proxy/mining.go b/server/proxy/mining.go index 1c7eeea..6666c30 100644 --- a/server/proxy/mining.go +++ b/server/proxy/mining.go @@ -4,6 +4,7 @@ import ( "context" "errors" "strconv" + "time" "github.com/sourcegraph/jsonrpc2" "github.com/swordlet/xmrig2xdag/logger" @@ -115,6 +116,7 @@ func (m *Mining) Getjob(p PassThruParams, resp *Job) error { // But the coinhive miner doesn't care, it just doesn't keep up with submissions. func (m *Mining) Submit(p PassThruParams, resp *StatusReply) error { worker := m.getWorker(p.Context()) + worker.Conn().SetReadDeadline(time.Now().Add(45 * time.Second)) status, err := worker.Proxy().Submit(p) if err != nil { return err diff --git a/server/proxy/proxy.go b/server/proxy/proxy.go index 78e2368..ff24426 100644 --- a/server/proxy/proxy.go +++ b/server/proxy/proxy.go @@ -73,7 +73,7 @@ type Worker interface { // Disconnect closes the connection to the proxy from the worker. // Ideally it sets up the worker to try and reconnect to a new proxy through the director. Disconnect() - + Conn() net.Conn Close() NewJob(*Job) @@ -142,17 +142,17 @@ func NewProxy(id uint64) *Proxy { miniResult: math.MaxUint64, notify: make(chan []byte, 2), } - go p.deleteIdle() + // go p.deleteIdle() p.SS = stratum.NewServer() p.SS.RegisterName("mining", &Mining{}) return p } -func (p *Proxy) deleteIdle() { - <-time.After(35 * time.Second) - p.Delete() -} +// func (p *Proxy) deleteIdle() { +// <-time.After(35 * time.Second) +// p.Delete() +// } func (p *Proxy) Run(minerName string) { retryTimes := 3 @@ -289,8 +289,8 @@ func (p *Proxy) handleNotification(notif []byte) { } func (p *Proxy) connect(minerName string) error { - p.connMu.Lock() - defer p.connMu.Unlock() + // p.connMu.Lock() + // defer p.connMu.Unlock() var conn net.Conn var socks5Dialer proxy.Dialer @@ -443,31 +443,31 @@ func (p *Proxy) Close() { p.isClosed = true } -func (p *Proxy) Delete() { - p.connMu.Lock() - defer p.connMu.Unlock() - - if p.isClosed { - return - } - - if p.Conn != nil && p.Conn.ConnID > 0 { - return - } - - if p.worker != nil { - p.worker.Close() - } - close(p.done) - p.director.removeProxy(p.ID) - p.worker = nil - p.SS = nil - p.director = nil - - logger.Get().Printf("Proxy[%d] idle deleted", p.ID) - p.Conn = nil - p.isClosed = true -} +// func (p *Proxy) Delete() { +// p.connMu.Lock() +// defer p.connMu.Unlock() + +// if p.isClosed { +// return +// } + +// if p.Conn != nil && p.Conn.ConnID > 0 { +// return +// } + +// if p.worker != nil { +// p.worker.Close() +// } +// close(p.done) +// p.director.removeProxy(p.ID) +// p.worker = nil +// p.SS = nil +// p.director = nil + +// logger.Get().Printf("Proxy[%d] idle deleted", p.ID) +// p.Conn = nil +// p.isClosed = true +// } func (p *Proxy) handleSubmit(s *share) (err error) { defer func() { diff --git a/server/stratum/server.go b/server/stratum/server.go index 9d235b9..f5b7da2 100644 --- a/server/stratum/server.go +++ b/server/stratum/server.go @@ -5,8 +5,12 @@ import ( "encoding/json" "errors" "io" + "net/http" "net/rpc" "sync" + + "github.com/didip/tollbooth" + "github.com/swordlet/xmrig2xdag/config" ) type serverCodec struct { @@ -105,14 +109,14 @@ func (s *Server) ServeCodec(codec rpc.ServerCodec) { s.Server.ServeCodec(codec) } -// func (s *Server) HandleHTTP(rpcPath, debugPath string) { -// limit := config.Get().RateLimit -// if limit == 0 { -// limit = 1 -// } -// http.Handle(rpcPath, tollbooth.LimitFuncHandler(tollbooth.NewLimiter(float64(limit), nil), s.ServeHTTP)) -// // http.Handle(debugPath, debugHTTP{server}) -// } +func (s *Server) HandleHTTP(rpcPath, debugPath string) { + limit := config.Get().RateLimit + if limit == 0 { + limit = 1 + } + http.Handle(rpcPath, tollbooth.LimitFuncHandler(tollbooth.NewLimiter(float64(limit), nil), s.ServeHTTP)) + // http.Handle(debugPath, debugHTTP{server}) +} func (s *Server) ServeConn(ctx context.Context, conn io.ReadWriteCloser) { s.ServeCodec(NewDefaultServerCodecContext(ctx, conn)) diff --git a/server/tcp/server.go b/server/tcp/server.go index 589aa3a..9f9211e 100644 --- a/server/tcp/server.go +++ b/server/tcp/server.go @@ -8,7 +8,6 @@ import ( "github.com/swordlet/xmrig2xdag/config" "github.com/swordlet/xmrig2xdag/logger" - "golang.org/x/time/rate" ) func StartServer() { @@ -37,24 +36,24 @@ func StartServer() { " Listen failed with error: ", listenErr) return } - rl := config.Get().RateLimit - if rl == 0 { - rl = 1 - } - ra := rate.Every(25 * time.Millisecond) - limit := rate.NewLimiter(ra, rl) + // rl := config.Get().RateLimit + // if rl == 0 { + // rl = 1 + // } + // ra := rate.Every(25 * time.Millisecond) + // limit := rate.NewLimiter(ra, rl) for { conn, err := listener.Accept() if err != nil { logger.Get().Println("Unable to accept connection: ", err) } - // conn.SetDeadline(time.Now().Add(45 * time.Second)) + conn.SetReadDeadline(time.Now().Add(90 * time.Second)) - if !limit.Allow() { - logger.Get().Println("Out of rate limit:", rl, "per 100 ms.") - conn.Close() - continue - } + // if !limit.Allow() { + // logger.Get().Println("Out of rate limit:", rl, "per 25 ms.") + // conn.Close() + // continue + // } go SpawnWorker(conn) } } diff --git a/server/tcp/worker.go b/server/tcp/worker.go index 4449c15..1208255 100644 --- a/server/tcp/worker.go +++ b/server/tcp/worker.go @@ -104,7 +104,6 @@ func (w *Worker) NewJob(j *proxy.Job) { w.Disconnect() } // other actions? shut down worker? - // w.Conn().SetDeadline(time.Now().Add(45 * time.Second)) } func (w *Worker) expectedHashes() uint32 {