trojanx

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 12 Imported by: 0

README

TrojanX

Trojan server framework.

Example

package main

import (
	"crypto/tls"
	"github.com/7ten7/trojanx"
	"github.com/sirupsen/logrus"
	"log"
	"net"
	"net/http"
	"time"
)

func main() {
	go func() {
		server := &http.Server{
			Addr:         "127.0.0.1:80",
			ReadTimeout:  3 * time.Second,
			WriteTimeout: 3 * time.Second,
		}
		server.SetKeepAlivesEnabled(false)
		http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
			defer request.Body.Close()
			logrus.Debugln(request.RemoteAddr, request.RequestURI)
			host, _, _ := net.SplitHostPort(request.Host)
			switch host {
			default:
				writer.Header().Set("Connection", "close")
				writer.Header().Set("Referrer-Policy", "no-referrer")
				http.Redirect(writer, request, "https://www.baidu.com/", http.StatusFound)
			}
		})
		if err := server.ListenAndServe(); err != nil {
			log.Fatalln(err)
		}
	}()

	signed, _ := generateSelfSigned()
	config := &trojanx.TrojanConfig{
		Password: "password",
		TLSConfig: &trojanx.TLSConfig{
			MinVersion:  tls.VersionTLS13,
			MaxVersion:  tls.VersionTLS13,
			Certificate: signed,
		},
		ReverseProxyConfig: &trojanx.ReverseProxyConfig{
			Scheme: "http",
			Host:   "127.0.0.1",
			Port:   80,
		},
	}

	srv := trojanx.NewServer(
		trojanx.WithConfig(config),
		trojanx.WithLogger(&logrus.Logger{}),
	)
	if err := srv.ListenAndServe("tcp", ":443"); err != nil {
		logrus.Fatalln(err)
	}
}

LICENSE

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CertificateFileConfig

type CertificateFileConfig struct {
	PublicKeyFile  string `json:"public_key_file"`
	PrivateKeyFile string `json:"private_key_file"`
}

type Option

type Option func(s *Server)

func WhichAuthenticationHandler

func WhichAuthenticationHandler(handler authenticationHandler) Option

func WhichErrorHandler

func WhichErrorHandler(handler errorHandler) Option

func WhichRequestHandler

func WhichRequestHandler(handler requestHandler) Option

func WithConfig

func WithConfig(config *TrojanConfig) Option

func WithConnectHandler

func WithConnectHandler(handler connectHandler) Option

func WithDial added in v0.0.2

func WithDial(dial func(ctx context.Context, network, addr string) (net.Conn, error)) Option

func WithLogger

func WithLogger(l *logrus.Logger) Option

type ReverseProxyConfig

type ReverseProxyConfig struct {
	RemoteURL string `json:"remote_url"`
	Scheme    string `json:"scheme"`
	Host      string `json:"host"`
	Port      int    `json:"port"`
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ...Option) *Server

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(network, addr string) error

func (*Server) ServeConn

func (s *Server) ServeConn(conn net.Conn)

type TLSConfig

type TLSConfig struct {
	MinVersion  uint16 `json:"min_version"`
	MaxVersion  uint16 `json:"max_version"`
	Certificate tls.Certificate
}

type TrojanConfig

type TrojanConfig struct {
	Password           string              `json:"password"`
	TLSConfig          *TLSConfig          `json:"tls_config"`
	ReverseProxyConfig *ReverseProxyConfig `json:"reverse_proxy"`
}

Directories

Path Synopsis
Package selfsign is a test helper that generates self signed certificate.
Package selfsign is a test helper that generates self signed certificate.
internal

Jump to

Keyboard shortcuts

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