Skip to content

Commit

Permalink
No mux
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jan 29, 2025
1 parent 3a0a648 commit 4462152
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 59 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/klauspost/compress v1.17.11
github.com/tdewolff/parse/v2 v2.7.19
github.com/tidwall/gjson v1.18.0
github.com/xtaci/smux v1.5.33
go.etcd.io/bbolt v1.3.11
golang.org/x/image v0.22.0
golang.org/x/net v0.31.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/xtaci/smux v1.5.33 h1:xosoZt0AUZdIXEB6z09kt1bge+l1L8wzMtJdPB6GAPI=
github.com/xtaci/smux v1.5.33/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
Expand Down
92 changes: 36 additions & 56 deletions handlers/scraper/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/kelindar/binary"
"github.com/xtaci/smux"
)

type remoteResult struct {
Expand Down Expand Up @@ -60,66 +59,47 @@ func InitRemoteScraper(listenAddr *net.TCPAddr, authCode []byte) error {
}

func handleConnection(conn net.Conn) {
smuxConfig := smux.DefaultConfig()
smuxConfig.Version = 2
var wg sync.WaitGroup

session, err := smux.Server(conn, smuxConfig)
if err != nil {
return
}
defer session.Close()
defer sessCount.Add(-1)
defer func() {
conn.Close()
wg.Done()
}()

for rm := range inChan {
var err error
if err := conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
slog.Error("failed to set deadline", "err", err)
rm.outChan <- err
return
}

sessCount.Add(1)
var wg sync.WaitGroup
for {
stream, err := session.AcceptStream()
buf := []byte(rm.instaData.PostID)
if _, err = conn.Write(buf); err != nil {
slog.Error("failed to write to stream", "err", err)
rm.outChan <- err
return
}

outBuf := make([]byte, 1024*1024)
n, err := conn.Read(outBuf)
if err != nil {
break
slog.Error("failed to read from stream", "err", err)
rm.outChan <- err
return
}

wg.Add(1)
go func(stream *smux.Stream) {
defer func() {
stream.Close()
wg.Done()
}()

for rm := range inChan {
if err := stream.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
slog.Error("failed to set deadline", "err", err)
rm.outChan <- err
return
}

buf := []byte(rm.instaData.PostID)
if _, err = stream.Write(buf); err != nil {
slog.Error("failed to write to stream", "err", err)
rm.outChan <- err
return
}

outBuf := make([]byte, 1024*1024)
n, err := stream.Read(outBuf)
if err != nil {
slog.Error("failed to read from stream", "err", err)
rm.outChan <- err
return
}

if err = binary.Unmarshal(outBuf[:n], rm.instaData); err != nil {
slog.Error("failed to unmarshal data", "err", err)
rm.outChan <- err
continue
}

if rm.instaData.Username == "" {
rm.outChan <- errors.New("remote scraper returns empty data")
continue
}
rm.outChan <- nil
}
}(stream)
if err = binary.Unmarshal(outBuf[:n], rm.instaData); err != nil {
slog.Error("failed to unmarshal data", "err", err)
rm.outChan <- err
continue
}

if rm.instaData.Username == "" {
rm.outChan <- errors.New("remote scraper returns empty data")
continue
}
rm.outChan <- nil
}

wg.Wait()
Expand Down

0 comments on commit 4462152

Please sign in to comment.