qsocket

package module
v0.0.8-beta Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: MIT Imports: 17 Imported by: 2

README

QSocket Go

Go library for qsocket...

Documentation

GoDoc Go Report Card

[!WARNING]
This library is in its early alpha development stage, featuring experimental functionality that may lack backwards compatibility, and users are advised to exercise caution and not use it in production environments.

Example

Usage is really simple, qsocket.New() function simply creates a new quantum socket with given secret, it includes all the functions of standard net sockets and also implements io Read/Write. After creating a socket you need to dial the QSRN network by calling Dial* functions. Simple example below...

    // Create a new QSocket client...
    qsock := qsocket.New(qsocket.Client, "my-secret");
    // Create a new QSocket server...
    qsock := qsocket.New(qsocket.Server, "my-secret");
    
    qsock.Dial(true)  // Dial using TLS...
    // OR
    qsock.Dial(false) // Dial using TCP... 

    // Dial using a socks5 proxy over TLS
    qsock.SetProxy("127.0.0.1:9050")
    qsock.Dial(true)

After dialing the QSRN, socket is ready for read/write operations. Check here and qs-netcat for more usage examples.

Documentation

Index

Constants

View Source
const (
	// QSRN_GATE is the static gate address for the QSocket network.
	QSRN_GATE = "relay.qsocket.io"
	// QSRN_TOR_GATE is the static ONION address for the QSocket network.
	QSRN_TOR_GATE = "5cah65fto4tjklhocryenlgti6bfnh4y5szjfvxeqqh3vvw2ff4uq2id.onion"
	// QSRN_GATE_TLS_PORT Default TLS port for the QSocket gate.
	QSRN_GATE_TLS_PORT = 443
	// QSRN_GATE_PORT Default TCP port for the QSocket gate.
	QSRN_GATE_PORT = 80
	// CHECKSUM_BASE is the constant base value for calculating knock sequence URI checksums.
	CHECKSUM_BASE = 0xEE
	URI_CHARSET   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
	CRLF          = "\r\n"
)

Some global constants for These values can be changed for obfuscating the knock protocol

View Source
const SRP_BITS = 4096
View Source
const UserAgentTemplate = "Mozilla/5.0 (%s; %s) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.3"

Variables

View Source
var (
	ErrFailedReadingProtocolSwitchResponse = errors.New("Failed reading protocol switch response.")
	ErrInvalidProtocolSwitchResponse       = errors.New("Invalid protocol switch response.")
	ErrProtocolSwitchFailed                = errors.New("Websocket protocol switch failed.")
	ErrServerCollision                     = errors.New("Address already in use. (server secret collision)")
	ErrPeerNotFound                        = errors.New("Connection refused. (no server listening with given secret)")
	ErrUpgradeRequired                     = errors.New("Protocol upgrade required!")

	HttpResponseRgx    = regexp.MustCompile(`^HTTP/([0-9]|[0-9]\.[0-9]) ([0-9]{1,3}) [a-z A-Z]+`)
	WebsocketAcceptRgx = regexp.MustCompile(`Sec-WebSocket-Accept: ([A-Za-z0-9+/]+={0,2})`)
)
View Source
var (
	Version                   = "?"
	ErrUntrustedCert          = errors.New("Certificate fingerprint mismatch!")
	ErrUninitializedSocket    = errors.New("Socket not initiated,")
	ErrQSocketSessionEnd      = errors.New("QSocket session has ended.")
	ErrUnexpectedSocket       = errors.New("Unexpected socket type.")
	ErrInvalidIdTag           = errors.New("Invalid peer ID tag.")
	ErrNoTlsConnection        = errors.New("TLS socket is nil.")
	ErrSocketNotConnected     = errors.New("Socket is not connected.")
	ErrSrpFailed              = errors.New("SRP auth failed.")
	ErrSocketInUse            = errors.New("Socket already dialed.")
	ErrInvalidCertFingerprint = errors.New("Invalid TLS certificate fingerprint.")
	//
	TOR_MODE = false
)

Functions

func BindSockets

func BindSockets(con1, con2 *QSocket) error

BindSockets is used for creating a full duplex channel between `con1` and `con2` sockets, effectively binding two sockets.

func CalcChecksum

func CalcChecksum(data []byte, base byte) byte

CalcChecksum calculates the modulus based checksum of the given data, modulus base is given in the base variable.

func CreateSocketChan

func CreateSocketChan(sock *QSocket) chan []byte

chanFromConn creates a channel from a Conn object, and sends everything it

Read()s from the socket to the channel.

func GetDeviceUserAgent

func GetDeviceUserAgent() string

func NewChecksumUri

func NewChecksumUri(sType SocketType) string

func RandomString

func RandomString(charset string, length int) string

Types

type QSocket

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

A QSocket structure contains required values for performing a knock sequence with the QSRN gate.

`Secret` value can be considered as the password for the QSocket connection, It will be used for generating a 128bit unique identifier (UID) for the connection.

`*tag` values are used internally for QoS purposes. It specifies the operating system, architecture and the type of connection initiated by the peers, the relay server uses these values for optimizing the connection performance.

func NewSocket

func NewSocket(sType SocketType, secret string) *QSocket

NewSocket creates a new QSocket structure with the given secret. `certVerify` value is used for enabling the certificate validation on TLS connections

func (*QSocket) Close

func (qs *QSocket) Close()

Close closes the QSocket connection and underlying TCP/TLS connections.

func (*QSocket) Dial

func (qs *QSocket) Dial(useTls bool) error

Dial creates a TLS connection to the `QSRN_GATE` on `QSRN_GATE_TLS_PORT`. Based on the `VerifyCert` parameter, certificate fingerprint validation (a.k.a. SSL pinning) will be performed after establishing the TLS connection.

func (*QSocket) DoWsProtocolSwitch

func (qs *QSocket) DoWsProtocolSwitch() error

SendKnockSequence sends a knock sequence to the QSRN gate with the socket properties.

func (*QSocket) InitClientSRP

func (qs *QSocket) InitClientSRP() ([]byte, error)

InitClientSRP performs the client SRP sequence for establishing PAKE.

func (*QSocket) InitE2ECipher

func (qs *QSocket) InitE2ECipher(key []byte) error

InitE2ECipher initiates the end-to-end encrypted stream with the given key.

func (*QSocket) InitServerSRP

func (qs *QSocket) InitServerSRP() ([]byte, error)

InitServerSRP performs the server SRP sequence for establishing PAKE.

func (*QSocket) InitiateKnockSequence

func (qs *QSocket) InitiateKnockSequence() error

func (*QSocket) IsClient

func (qs *QSocket) IsClient() bool

IsClient checks if the QSocket connection is initiated as a client or a server.

func (*QSocket) IsClosed

func (qs *QSocket) IsClosed() bool

IsClosed checks if the QSocket connection to the `QSRN_GATE` is ended.

func (*QSocket) IsE2E

func (qs *QSocket) IsE2E() bool

IsE2E checks if the underlying connection is E2E encrypted or not.

func (*QSocket) IsServer

func (qs *QSocket) IsServer() bool

IsClient checks if the QSocket connection is initiated as a client or a server.

func (*QSocket) IsTLS

func (qs *QSocket) IsTLS() bool

IsTLS checks if the underlying connection is TLS or not.

func (*QSocket) LocalAddr

func (qs *QSocket) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*QSocket) Read

func (qs *QSocket) Read(b []byte) (int, error)

Read reads data from the connection.

As Read calls Handshake, in order to prevent indefinite blocking a deadline must be set for both Read and Write before Read is called when the handshake has not yet completed. See SetDeadline, SetReadDeadline, and SetWriteDeadline.

func (*QSocket) RemoteAddr

func (qs *QSocket) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*QSocket) SetCertFingerprint

func (qs *QSocket) SetCertFingerprint(h string) error

AddIdTag adds a peer identification tag to the QSocket.

func (*QSocket) SetE2E

func (qs *QSocket) SetE2E(v bool) error

AddIdTag adds a peer identification tag to the QSocket.

func (*QSocket) SetProxy

func (qs *QSocket) SetProxy(proxyAddr string) error

AddIdTag adds a peer identification tag to the QSocket.

func (*QSocket) SetReadDeadline

func (qs *QSocket) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline on the underlying connection. A zero value for t means Read will not time out.

func (*QSocket) SetWriteDeadline

func (qs *QSocket) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline on the underlying connection. A zero value for t means Write will not time out. After a Write has timed out, the TLS state is corrupt and all future writes will return the same error. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.

func (*QSocket) VerifyTlsCertificate

func (qs *QSocket) VerifyTlsCertificate() error

func (*QSocket) Write

func (qs *QSocket) Write(b []byte) (int, error)

Write writes data to the connection.

As Write calls Handshake, in order to prevent indefinite blocking a deadline must be set for both Read and Write before Write is called when the handshake has not yet completed. See SetDeadline, SetReadDeadline, and SetWriteDeadline.

type SocketType

type SocketType byte
const (
	// Tag ID for representing server mode connections.
	Server SocketType = iota // 00000000 => Server
	// Tag ID for representing client mode connections.
	Client
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳