socksauth

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 11 Imported by: 0

README

SocksAuth

This project forwards your TCP connection through a SOCKS5 proxy, especially it can be used to forward it through a SOCKS5 proxy with authentication.

I tried to keep it minimal. It has 0 depencencies,

Why?

Because I needed a way to use a propietery SOCKS5 server with an automated browser, which does not allow to pass authentification to proxies.

As executable

You can install it as executable

git clone https://https://github.com/FrauElster/SocksAuth.git && cd ./SocksAuth
cd src && go build -o ../socksauth ./server && cd .. && rm 

And run it with

./socksauth -remoteUser <username> -remotePass <password> [-remoteHost <host:port>] [-port <localport>]

If the remoteHost is omitted a NordVPN will be used (because that was my usecase).

If the port is omitted, 1080 will be used.

As module

This is pretty much just the main.go of the server

package main

import (
	"context"
	"fmt"
	"log"
	"log/slog"
	"net"
	"os"
	"os/signal"

	"github.com/FrauElster/socksauth"
)

func main() {
	// build the server
	onError := func(connId int64, conn net.Conn, err error) { slog.Error("Error", "connId", connId, "err", err) }
	onConnect := func(connId int64, conn net.Conn) {
		slog.Debug("Connected", "connId", connId, "addr", conn.RemoteAddr())
	}
	onDisconnect := func(connId int64, conn net.Conn) {
		slog.Debug("Disconnected", "connId", connId, "addr", conn.RemoteAddr())
	}
	server := socksauth.NewServer(remoteHost, remoteUser, remotePass,
		socksauth.WithAddr(fmt.Sprintf(":%d", port)), socksauth.WithOnConnect(onConnect), socksauth.WithOnDisconnect(onDisconnect), socksauth.WithOnError(onError))

	// Start the server
	runCtx, cancel := context.WithCancel(context.Background())
	err := server.Start(runCtx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("SOCKS5 server is listening on ", server.Addr)

	// wait for ctrl+c
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt)
	<-sigChan
	fmt.Println("Shutting down...")
	cancel()
}

or more minimal

server := socksauth.NewServer(remoteHost, remoteUser, remotePass)
err := server.Start(context.Background())
if err != nil {
	log.Fatal(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindNordVpnServer

func FindNordVpnServer(ctx context.Context) (host string, err error)

FindNordVpnServer finds a socks server from the (undocumented) NordVPN API

Types

type Server

type Server struct {
	Addr string

	RemoteHost string
	RemoteUser string
	RemotePass string

	ConnCount     atomic.Int64
	OpenConnCount atomic.Int32
	// contains filtered or unexported fields
}

func NewServer

func NewServer(remoteHost, remoteUser, remotePass string, opts ...ServerOption) *Server

NewServer creates a new SOCKS5 server if the remoteHost is empty the server will try to find a server using the serverFinder function specified in the WithServerFinder option (default is FindNordVpnServer) if the remoteUser and remotePass are empty the server will not authenticate with the remote server, so it is just a simple SOCKS5 proxy, no auth.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

type ServerOption

type ServerOption func(*Server)

func WithAddr

func WithAddr(addr string) ServerOption

WithAddr sets the address the server will listen on Default is ":1080"

func WithOnConnect

func WithOnConnect(fn func(id int64, conn net.Conn)) ServerOption

WithOnConnect sets the onConnect callback which is called when a new connection is accepted To not block the server the callback is called in a new goroutine

func WithOnDisconnect

func WithOnDisconnect(fn func(id int64, conn net.Conn)) ServerOption

WithOnDisconnect sets the onDisconnect callback which is called when a connection is closed To not block the server the callback is called in a new goroutine

func WithOnError

func WithOnError(fn func(id int64, conn net.Conn, err error)) ServerOption

WithOnError sets the onError callback which is called when an error occurs on a connection. If the error occurs while accepting a connection the id is 0 and the conn argument will be nil To not block the server the callback is called in a new goroutine

func WithServerFinder

func WithServerFinder(fn func(context.Context) (string, error)) ServerOption

WithServerFinder sets the function to find a SOCKS5 server if no remoteHost is provided Default will find a NordVPN server and try to authenticate with it

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL