github.com/iDigitalFlame/xmt@v0.5.4/com/vars.go (about) 1 // Copyright (C) 2020 - 2023 iDigitalFlame 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 // 16 17 // Package com contains many helper functions for network communications. This 18 // package includes some constant types that can be used with the "c2" package. 19 package com 20 21 import ( 22 "context" 23 "crypto/tls" 24 "net" 25 "time" 26 ) 27 28 // DefaultTimeout is the default timeout used for the default connectors. 29 // The default is 15 seconds. 30 const DefaultTimeout = time.Second * 15 // 30 31 32 // ListenConfig is the default listener config that is used to generate the 33 // Listeners. This can be used to specify the listen 'KeepAlive' timeout. 34 var ListenConfig = newListenConfig(DefaultTimeout) 35 36 var ( 37 // TCP is the TCP Raw connector. This connector uses raw TCP connections for 38 // communication. 39 TCP = NewTCP(DefaultTimeout) 40 41 // UDP is the UDP Raw connector. This connector uses raw UDP connections for 42 // communication. 43 UDP = NewUDP(DefaultTimeout) 44 45 // ICMP is the ICMP Raw connector. This connector uses raw ICMP connections 46 // for communication. 47 // 48 // TODO(dij): I think ICMP is bugged ATM, "NewIP(<anything greater than 1>, DefaultTimeout)" works, weird. 49 ICMP = NewIP(DefaultTimeout, 1) 50 51 // TLS is the TCP over TLS connector client. This client uses TCP wrapped in 52 // TLS encryption using certificates. 53 // 54 // This client is only valid for clients that connect to servers with properly 55 // signed and trusted certificates. 56 TLS = tcpClient{c: tcpConnector{tls: &tls.Config{MinVersion: tls.VersionTLS12}, Dialer: TCP.(*tcpConnector).Dialer}} 57 58 // TLSInsecure is the TCP over TLS connector profile. This client uses TCP 59 // wrapped in TLS encryption using certificates. 60 // 61 // This instance DOES NOT check the server certificate for validity. 62 TLSInsecure = tcpClient{c: tcpConnector{tls: &tls.Config{MinVersion: tls.VersionTLS11, InsecureSkipVerify: true}, Dialer: TCP.(*tcpConnector).Dialer}} 63 ) 64 65 type deadliner interface { 66 SetDeadline(time.Time) error 67 } 68 69 // Connector is an interface that represents an object that can create and 70 // establish connections on various protocols. 71 type Connector interface { 72 Connect(context.Context, string) (net.Conn, error) 73 Listen(context.Context, string) (net.Listener, error) 74 } 75 76 // DialTCP is a quick utility function that can be used to quickly create a TCP 77 // connection to the provided address. 78 // 79 // This function uses the 'com.TCP' var. 80 func DialTCP(x context.Context, s string) (net.Conn, error) { 81 return TCP.Connect(x, s) 82 } 83 84 // ListenTCP is a quick utility function that can be used to quickly create a 85 // TCP listener using the 'TCP' Acceptor. 86 func ListenTCP(x context.Context, s string) (net.Listener, error) { 87 return TCP.Listen(x, s) 88 } 89 90 // DialTLS is a quick utility function that can be used to quickly create a TLS 91 // connection to the provided address. 92 func DialTLS(x context.Context, s string, c *tls.Config) (net.Conn, error) { 93 if c == nil { 94 return TLS.Connect(x, s) 95 } 96 return TLS.ConnectConfig(x, c, s) 97 } 98 99 // SetListenerDeadline attempts to set a deadline on the 'Accept; function of a 100 // Listener if applicable. This function will return any errors if they occur 101 // and always returns 'nil' if the Listener does not support deadlines. 102 func SetListenerDeadline(l net.Listener, t time.Time) error { 103 if d, ok := l.(deadliner); ok { 104 return d.SetDeadline(t) 105 } 106 return nil 107 } 108 109 // ListenTLS is a quick utility function that can be used to quickly create a TLS 110 // listener using the provided TLS config. 111 func ListenTLS(x context.Context, s string, c *tls.Config) (net.Listener, error) { 112 return newStreamListener(x, NameTCP, s, &tcpConnector{tls: c, Dialer: TCP.(*tcpConnector).Dialer}) 113 }