Add support for IRC address schemes
This is preparatory work for adding other connection types to upstream servers. The service command `network create` now accepts a scheme in the address flag, which specifies how to connect to the upstream server. The only supported scheme for now is ircs, which is also the default if no scheme is specified. ircs connects to a network over a TLS TCP connection.
This commit is contained in:
parent
19854b7ec7
commit
19795a2321
@ -93,7 +93,12 @@ abbreviated form, for instance *network* can be abbreviated as *net* or just
|
|||||||
the command.
|
the command.
|
||||||
|
|
||||||
*network create* *-addr* <addr> [options...]
|
*network create* *-addr* <addr> [options...]
|
||||||
Connect to a new network at _addr_. _-addr_ is mandatory. Other options are:
|
Connect to a new network at _addr_. _-addr_ is mandatory.
|
||||||
|
|
||||||
|
_addr_ supports several connection types:
|
||||||
|
- _[ircs://]host[:port]_ connects with TLS over TCP
|
||||||
|
|
||||||
|
Other options are:
|
||||||
|
|
||||||
*-name* <name>
|
*-name* <name>
|
||||||
Short network name. This will be used instead of _addr_ to refer to the
|
Short network name. This will be used instead of _addr_ to refer to the
|
||||||
|
@ -203,6 +203,15 @@ func handleServiceCreateNetwork(dc *downstreamConn, params []string) error {
|
|||||||
return fmt.Errorf("flag -addr is required")
|
return fmt.Errorf("flag -addr is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if addrParts := strings.SplitN(*addr, "://", 2); len(addrParts) == 2 {
|
||||||
|
scheme := addrParts[0]
|
||||||
|
switch scheme {
|
||||||
|
case "ircs":
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown scheme %q (supported schemes: ircs)", scheme)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, command := range connectCommands {
|
for _, command := range connectCommands {
|
||||||
_, err := irc.ParseMessage(command)
|
_, err := irc.ParseMessage(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
26
upstream.go
26
upstream.go
@ -66,15 +66,33 @@ type upstreamConn struct {
|
|||||||
func connectToUpstream(network *network) (*upstreamConn, error) {
|
func connectToUpstream(network *network) (*upstreamConn, error) {
|
||||||
logger := &prefixLogger{network.user.srv.Logger, fmt.Sprintf("upstream %q: ", network.Addr)}
|
logger := &prefixLogger{network.user.srv.Logger, fmt.Sprintf("upstream %q: ", network.Addr)}
|
||||||
|
|
||||||
addr := network.Addr
|
var scheme string
|
||||||
if !strings.ContainsRune(addr, ':') {
|
var addr string
|
||||||
addr = addr + ":6697"
|
|
||||||
|
addrParts := strings.SplitN(network.Addr, "://", 2)
|
||||||
|
if len(addrParts) == 2 {
|
||||||
|
scheme = addrParts[0]
|
||||||
|
addr = addrParts[1]
|
||||||
|
} else {
|
||||||
|
scheme = "ircs"
|
||||||
|
addr = addrParts[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
dialer := net.Dialer{Timeout: connectTimeout}
|
dialer := net.Dialer{Timeout: connectTimeout}
|
||||||
|
|
||||||
|
var netConn net.Conn
|
||||||
|
var err error
|
||||||
|
switch scheme {
|
||||||
|
case "ircs":
|
||||||
|
if !strings.ContainsRune(addr, ':') {
|
||||||
|
addr = addr + ":6697"
|
||||||
|
}
|
||||||
|
|
||||||
logger.Printf("connecting to TLS server at address %q", addr)
|
logger.Printf("connecting to TLS server at address %q", addr)
|
||||||
netConn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
|
netConn, err = tls.DialWithDialer(&dialer, "tcp", addr, nil)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("failed to dial %q: unknown scheme: %v", addr, scheme)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user