A little library for turning TCP connections into go channels.

netutils

By Tim Henderson ([email protected])

This is a little library that was part of a larger project that I decided to pull out and make public. It is kinda a replacement for netchan in the sense that it allows you to treat a TCP connection as a pair of chans. However, it is not as ambitious as netchan was. It doesn't attempt to solve any sort of serialization for you instead it is a []byte oriented interface.

How It Works

It has two important methods which can take a TCP connection and turn it into a pair of channels. The first is TCPReader:

func TCPReader(con *net.TCPConn) (recv <-chan byte) 

TCPReader emits a chan byte which you can use to read the TCP connection in a byte oriented manner. Internally, it is maintaining a buffer but externally it presents it as a smooth stream. If you want to process the connection in a line or other delimiter oriented way there are utility methods to assist you.

The second method is TCPWriter which is almost the inverse of TCPReader:

func TCPWriter(con *net.TCPConn) (send chan<- []byte)

Instead of creating a byte oriented channel it creates a []byte oriented channel. This maps more smoothly to how sending is usually done, by sending preconstructed packets of data. If you are interested in sending a free form stream of bytes you probably want to use the TCP connection object natively.

Docs

PACKAGE DOCUMENTATION

package netutils
    import "github.com/timtadh/netutils"



VARIABLES

var DEBUG bool = false


FUNCTIONS

func IsEOF(err error) bool
    A utility function which tests if an error returned from a TCPConnection
    or TCPListener is actually an EOF. In some edge cases this which should
    be treated as EOFs are not returned as one.

func ReadDelim(recv <-chan byte, delim byte) (line []byte, EOF bool)
    A blocking read operation on a byte channel. It reads up to a delimiter
    and returns a byte slice of the bytes read (not including the delim). If
    it encounters an EOF (a closed channel) it returns true on EOF and
    whatever was read before the EOF.

func ReadDelims(recv <-chan byte, delim byte) (linechan <-chan []byte)
    ReadDelims is a nonblocking reader on a byte channel. It consumes the
    channel (eg. will read until the recv channel closes). You should not do
    other reads on the recv channel if using ReadDelims.

    ReadDelims reads until a deliminter it encountered. It then emits a byte
    slice which does not include the deliminter. The sending channel will be
    closed when the recieving channel is closed.

func Reader(reader io.Reader, errors chan<- error) <-chan byte

func Readline(recv <-chan byte) (line []byte, EOF bool)
    A block read operation which reads one line from the byte channel. It
    used ReadDelim(recv, '\n') under the hood.

func Readlines(recv <-chan byte) (linechan <-chan []byte)
    Readlines is a nonblock reader on a byte channel. It reads all the lines
    from the recieving channel. See the ReadDelims documentation for caveats
    on usage (as it uses ReadDelims under the hood).

func TCPReader(con *net.TCPConn, errors chan<- error) (recv <-chan byte)
    A TCPReader takes a tcp connection and transforms it into a readable
    byte channel. It doesn't read []byte since it doesn't know what
    delimiters you want to use. If you want to consume the byte channel as a
    series of lines try "Readlines" if you have another delimiter ('\0' or
    similar) try ReadDelims.

func TCPWriter(con *net.TCPConn, errors chan<- error) (send chan<- []byte)
    A TCPWriter takes a tcp connection and transforms it into a writable
    []byte channel. You can use this to build more "goish" tcp servers.

func Writer(writer io.WriteCloser, errors chan<- error) chan<- []byte

Owner
Tim Henderson
Hacker / Software Engineering and Data Mining Researcher from CWRU. Now at Google. keybase.io/tadh
Tim Henderson
Similar Resources

List of youtube live channels videos

LIVE Youtube has a bug in their playlist feature when adding a live channel video to it. The channel live will stop at some point of time and the live

Dec 25, 2021

v2 of the veai listener. Now with channels!

veai-listener This is a pipeline tool to allow for watching a dir that takes in video that needs to be upscaled. The dir is accessed via Dropbox and i

Dec 8, 2021

Gogrok is a self hosted, easy to use alternative to ngrok. It uses SSH as a base protocol, using channels and existing functionality to tunnel requests to an endpoint.

gogrok A simple, easy to use ngrok alternative (self hosted!) The server and client can also be easily embedded into your applications, see the 'serve

Dec 3, 2022

handling 1M websockets connections in Go

Going Infinite, handling 1M websockets connections in Go This repository holds the complete implementation of the examples seen in Gophercon Israel ta

Jan 8, 2023

rconn is a multiplatform program for creating generic reverse connections. Lets you consume services that are behind firewall or NAT without opening ports or port-forwarding.

rconn is a multiplatform program for creating generic reverse connections. Lets you consume services that are behind firewall or NAT without opening ports or port-forwarding.

rconn (r[everse] conn[ection]) is a multiplatform program for creating reverse connections. It lets you consume services that are behind NAT and/or fi

Jan 1, 2023

UDP Transport: compress, encrypt and send any data reliably over unreliable UDP connections

udpt UDP Transport Compresses, encrypts and transfers data between a sender and receiver using UDP protocol. Features and Design Aims: Avoid the overh

Nov 5, 2022

A golang universal connections pool.

gopool A golang universal connection pool. Features: Using arrays instead of channels to improve performance Automatically detect live server nodes an

Jul 15, 2022

Designed to support DNS brute-forcing with a minimal number of network connections

Fast Use of DNS Resolvers Designed to support DNS brute-forcing with a minimal number of network connections. Installation go get -v -u github.com/caf

Dec 8, 2022

🧮 SOCKS5/4/4a 🌾 validating proxy pool for 🤽 LOLXDsoRANDum connections 🎋

🧮 SOCKS5/4/4a 🌾 validating proxy pool for 🤽 LOLXDsoRANDum connections 🎋

Prox5 SOCKS5/4/4a validating proxy pool This package is for managing and accessing thousands upon thousands of arbitrary SOCKS proxies. It also has a

Dec 25, 2022
Related tags
Multiplexer over TCP. Useful if target server only allows you to create limited tcp connections concurrently.

tcp-multiplexer Use it in front of target server and let your client programs connect it, if target server only allows you to create limited tcp conne

May 27, 2021
a go mini version TCP top on UDP for game connections or others.

sanhua sanhua(三花猫) is kind of cat with black, red and white color. This is a mini version TCP top on UDP, but with out resend lost packet. As we know.

May 31, 2022
TcpRoute , TCP 层的路由器。对于 TCP 连接自动从多个线路(电信、联通、移动)、多个域名解析结果中选择最优线路。

TcpRoute2 TcpRoute , TCP 层的路由器。对于 TCP 连接自动从多个线路(允许任意嵌套)、多个域名解析结果中选择最优线路。 TcpRoute 使用激进的选路策略,对 DNS 解析获得的多个IP同时尝试连接,同时使用多个线路进行连接,最终使用最快建立的连接。支持 TcpRoute

Dec 27, 2022
TCP output for beats to send events over TCP socket.

beats-tcp-output How To Use Clone this project to elastic/beats/libbeat/output/ Modify elastic/beats/libbeat/publisher/includes/includes.go : // add i

Aug 25, 2022
Tcp chat go - Create tcp chat in golang

TCP chat in GO libs Go net package and goroutines and channels tcp tcp or transm

Feb 5, 2022
Chat app that allows you to group multiple channels into one view.

hashchat Backend service Getting Started Essential steps to get your backend service deployed A helloworld example has been shipped with the template

Dec 13, 2021
wire protocol for multiplexing connections or streams into a single connection, based on a subset of the SSH Connection Protocol

qmux qmux is a wire protocol for multiplexing connections or streams into a single connection. It is based on the SSH Connection Protocol, which is th

Dec 26, 2022
Pusher Channels HTTP API library for Go

Pusher Channels HTTP Go Library The Golang library for interacting with the Pusher Channels HTTP API. This package lets you trigger events to your cli

Dec 19, 2022
Jun 6, 2022