<commit_before>package forward

import (
	"bufio"
	"net"
	"errors"
	"strings"
	"net/http"
	"net/http/httputil"
	
	"github.com/rookmoot/proxifier/logger"
)

type Forward struct {
	conn      net.Conn
	remote	  Remote

	log       logger.Logger

	request  *http.Request
	response *http.Response

	MaxRetry  int
	data     interface{}
}

type Remote interface {
	Close()
	GetConn() (*net.TCPConn, error)
}

type OnAuthenticationHandlerFunc func(req *http.Request, username string, password string) (bool, error)
type OnToHandlerFunc func(req *http.Request) (Remote, error)
type OnHandlerFunc func(resp *http.Response, req *http.Request) (error)

func New(conn net.Conn, log logger.Logger) (*Forward, error) {
	fwd := Forward{
		conn: conn,
		remote: nil,
		log: log,
		MaxRetry: 1,
	}
	return &fwd, nil
}

func (fwd *Forward)SetData(data interface{}) {
	fwd.data = data
}

func (fwd *Forward)GetData() interface{} {
	return fwd.data
}

func (fwd *Forward)Close() {
	fwd.conn.Close()
	if fwd.remote != nil {
		fwd.remote.Close()
	}
}

func (fwd *Forward)On(cb OnHandlerFunc) error {
	return cb(fwd.response, fwd.request)
}

func (fwd *Forward)OnAuthentication(cb OnAuthenticationHandlerFunc) (bool, error) {
	return cb(fwd.request, "test", "test")
}

func (fwd *Forward)To(cb OnToHandlerFunc) error {
	// read client request from socket
	// Here we can check for proxy authentication
	// and others headers sent like X-PROXIFIER-
	// then, we NEED to remove those header to
	// send only clean headers
	err := fwd.readRequest()
	if err != nil {
		return err
	}

	// use the callback func to get the remote
	// net tcp connection to forward requests to
	remote, err := cb(fwd.request)
	if err != nil {
		return err
	}	
	fwd.remote = remote<commit_msg>Adding User Interface to be able to force or auth user throught proxifier in order to make it a private web proxy.<commit_after>package forward

import (
	"bufio"
	"net"
	"errors"
	"strings"
	"net/http"
	"net/http/httputil"
	
	"github.com/rookmoot/proxifier/logger"
)

type Remote interface {
	Close()
	GetConn() (*net.TCPConn, error)
}

type User interface {
	IsConnected() (bool, error)
}

type Forward struct {
	log       logger.Logger

	conn      net.Conn
	remote	  Remote

	user	 *User
	
	request  *http.Request
	response *http.Response

	MaxRetry  int
	data     interface{}
}

type OnAuthenticationHandlerFunc func(req *http.Request, username string, password string) (*User, error)
type OnToHandlerFunc func(req *http.Request) (Remote, error)
type OnHandlerFunc func(resp *http.Response, req *http.Request) (error)

func New(conn net.Conn, log logger.Logger) (*Forward, error) {
	fwd := Forward{
		conn: conn,
		remote: nil,
		log: log,
		MaxRetry: 1,
	}
	return &fwd, nil
}

func (fwd *Forward)SetData(data interface{}) {
	fwd.data = data
}

func (fwd *Forward)GetData() interface{} {
	return fwd.data
}

func (fwd *Forward)SetUser(user *User) {
	fwd.user = user
}

func (fwd *Forward)GetUser() *User {
	return fwd.user
}

func (fwd *Forward)Close() {
	fwd.conn.Close()
	if fwd.remote != nil {
		fwd.remote.Close()
	}
}

func (fwd *Forward)On(cb OnHandlerFunc) error {
	return cb(fwd.response, fwd.request)
}

func (fwd *Forward)OnAuthentication(cb OnAuthenticationHandlerFunc) {
	_user, err := cb(fwd.request, "test", "test")
	if err != nil {
		fwd.log.Warn("Auth : %v", err)
		return
	}
	fwd.user = _user
}

func (fwd *Forward)To(cb OnToHandlerFunc) error {
	// read client request from socket
	// Here we can check for proxy authentication
	// and others headers sent like X-PROXIFIER-
	// then, we NEED to remove those header to
	// send only clean headers
	err := fwd.readRequest()
	if err != nil {
		return err
	}

	// use the callback func to get the remote
	// net tcp connection to forward requests to
	remote, err := cb(fwd.request)
	if err != nil {
		return err
	}	
	fwd.remote = remote
{'fraction_non_alphanumeric': 0.12214137214137215, 'fraction_numerical': 0.0005197505197505198, 'mean_word_length': 3.7754342431761785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4395578', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1361, 'n_words': 412}