mirror of
https://github.com/go-i2p/go-i2cp.git
synced 2025-06-16 12:05:45 -04:00
Finished sessions, only datagrams left
This commit is contained in:
115
tcp.go
115
tcp.go
@ -1,35 +1,114 @@
|
||||
package go_i2cp
|
||||
|
||||
import "net"
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func tcpInit() {
|
||||
address = net.TCPAddr{
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 7654,
|
||||
}
|
||||
type TcpProperty int
|
||||
|
||||
const (
|
||||
TCP_PROP_ADDRESS TcpProperty = iota
|
||||
TCP_PROP_PORT
|
||||
TCP_PROP_USE_TLS
|
||||
TCP_PROP_TLS_CLIENT_CERTIFICATE
|
||||
NR_OF_TCP_PROPERTIES
|
||||
)
|
||||
|
||||
var CAFile = "/etc/ssl/certs/ca-certificates.crt"
|
||||
var defaultRouterAddress = "127.0.0.1:7654"
|
||||
|
||||
const USE_TLS = true
|
||||
|
||||
func (tcp *Tcp) Init() (err error) {
|
||||
tcp.address, err = net.ResolveTCPAddr("tcp", defaultRouterAddress)
|
||||
return
|
||||
}
|
||||
|
||||
func tcpDeinit() {
|
||||
|
||||
}
|
||||
|
||||
var address net.TCPAddr
|
||||
var conn *net.TCPConn
|
||||
|
||||
func (tcp *Tcp) Connect() (err error) {
|
||||
conn, err = net.DialTCP("tcp", nil, &address )
|
||||
if USE_TLS {
|
||||
roots, err := x509.SystemCertPool()
|
||||
tcp.tlsConn, err = tls.Dial("tcp", tcp.address.String(), &tls.Config{RootCAs: roots})
|
||||
err = tcp.tlsConn.Handshake()
|
||||
} else {
|
||||
tcp.conn, err = net.DialTCP("tcp", nil, tcp.address)
|
||||
if err == nil {
|
||||
err = tcp.conn.SetKeepAlive(true)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tcp *Tcp) Send(buf *Stream) (i int, err error) {
|
||||
i, err = conn.Write(buf.Bytes())
|
||||
if USE_TLS {
|
||||
i, err = tcp.tlsConn.Write(buf.Bytes())
|
||||
} else {
|
||||
i, err = tcp.conn.Write(buf.Bytes())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tcp *Tcp) Receive(buf *Stream) (i int, err error) {
|
||||
i, err = conn.Read(buf.Bytes())
|
||||
if USE_TLS {
|
||||
i, err = tcp.tlsConn.Read(buf.Bytes())
|
||||
} else {
|
||||
i, err = tcp.conn.Read(buf.Bytes())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tcp *Tcp) CanRead() bool {
|
||||
var one []byte
|
||||
if USE_TLS {
|
||||
tcp.tlsConn.SetReadDeadline(time.Now())
|
||||
if _, err := tcp.tlsConn.Read(one); err == io.EOF {
|
||||
Debug(TCP, "%s detected closed LAN connection", tcp.address.String())
|
||||
defer tcp.Disconnect()
|
||||
return false
|
||||
} else {
|
||||
var zero time.Time
|
||||
tcp.tlsConn.SetReadDeadline(zero)
|
||||
return tcp.tlsConn.ConnectionState().HandshakeComplete
|
||||
}
|
||||
} else {
|
||||
tcp.conn.SetReadDeadline(time.Now())
|
||||
if _, err := tcp.conn.Read(one); err == io.EOF {
|
||||
Debug(TCP, "%s detected closed LAN connection", tcp.address.String())
|
||||
defer tcp.Disconnect()
|
||||
return false
|
||||
} else {
|
||||
var zero time.Time
|
||||
tcp.conn.SetReadDeadline(zero)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (tcp *Tcp) Disconnect() {
|
||||
if USE_TLS {
|
||||
tcp.tlsConn.Close()
|
||||
} else {
|
||||
tcp.conn.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (tcp *Tcp) IsConnected() bool {
|
||||
return tcp.CanRead()
|
||||
}
|
||||
|
||||
func (tcp *Tcp) SetProperty(property TcpProperty, value string) {
|
||||
tcp.properties[property] = value
|
||||
}
|
||||
func (tcp *Tcp) GetProperty(property TcpProperty) string {
|
||||
return tcp.properties[property]
|
||||
}
|
||||
|
||||
type Tcp struct {
|
||||
|
||||
}
|
||||
address *net.TCPAddr
|
||||
conn *net.TCPConn
|
||||
tlsConn *tls.Conn
|
||||
properties [NR_OF_TCP_PROPERTIES]string
|
||||
}
|
||||
|
Reference in New Issue
Block a user