Skip to content

Commit

Permalink
Add auth to remote scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jan 27, 2025
1 parent 7b2af6f commit 9a45879
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
29 changes: 23 additions & 6 deletions handlers/scraper/remote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"errors"
"log/slog"
"net"
Expand All @@ -20,25 +21,41 @@ type remoteResult struct {
var sessCount atomic.Int32
var inChan chan remoteResult

func init() {
func InitRemoteScraper(listenAddr *net.TCPAddr, authCode []byte) error {
if len(authCode) > 8 {
return errors.New("auth code max length is 8 bytes")
}

inChan = make(chan remoteResult)

ln, err := net.Listen("tcp", "0.0.0.0:4444")
ln, err := net.ListenTCP("tcp", listenAddr)
if err != nil {
return
return err
}
slog.Info("remote scraper is listening on", "address", ln.Addr())

go func() {
go func(ln *net.TCPListener, authCode []byte) {
for {
conn, err := ln.Accept()
if err != nil {
return
conn.Close()
continue
}

// deadline for read 5s
conn.SetReadDeadline(time.Now().Add(5 * time.Second))

authBytes := make([]byte, 8)
n, err := conn.Read(authBytes)
if err != nil || !bytes.Equal(authBytes[:n], authCode) {
conn.Close()
continue
}

go handleConnection(conn)
}
}()
}(ln, authCode)
return err
}

func handleConnection(conn net.Conn) {
Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"instafix/utils"
"instafix/views"
"log/slog"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand All @@ -28,6 +29,8 @@ func main() {
listenAddr := flag.String("listen", "0.0.0.0:3000", "Address to listen on")
gridCacheMaxFlag := flag.String("grid-cache-entries", "1024", "Maximum number of grid images to cache")
videoProxyAddr := flag.String("video-proxy-addr", "", "Video proxy address (https://github.com/Wikidepia/InstaFix-proxy)")
remoteAddr := flag.String("remote-scraper-addr", "0.0.0.0:3001", "Address of remote scraper")
authCode := flag.String("remote-scraper-auth-code", "", "Authentication code for remote scraper")
flag.Parse()

// Initialize video proxy
Expand Down Expand Up @@ -55,6 +58,18 @@ func main() {
scraper.InitDB()
defer scraper.DB.Close()

// Initialize remote scraper
if len(*authCode) != 0 {
remoteTCPAddr, err := net.ResolveTCPAddr("tcp", *remoteAddr)
if err != nil {
panic(err)
}
err = scraper.InitRemoteScraper(remoteTCPAddr, []byte(*authCode))
if err != nil {
panic(err)
}
}

// Evict cache every minute
go func() {
for {
Expand Down

0 comments on commit 9a45879

Please sign in to comment.