2016-01-04 18:26:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-01-11 20:04:57 +00:00
|
|
|
"crypto/tls"
|
2018-08-10 18:24:29 +00:00
|
|
|
"encoding/hex"
|
2019-01-25 10:02:31 +00:00
|
|
|
"fmt"
|
2020-06-16 01:04:27 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2016-01-11 20:04:57 +00:00
|
|
|
|
2020-12-07 06:46:53 +00:00
|
|
|
// "github.com/cryptix/goSam"
|
2020-07-30 02:28:34 +00:00
|
|
|
"github.com/eyedeekay/goSam"
|
2018-05-31 21:24:59 +00:00
|
|
|
"github.com/khlieng/dispatch/pkg/irc"
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
2020-06-16 01:04:27 +00:00
|
|
|
"golang.org/x/net/proxy"
|
2016-01-04 18:26:32 +00:00
|
|
|
)
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
func createNickInUseHandler(i *irc.Client, state *State) func(string) string {
|
2017-04-06 21:26:58 +00:00
|
|
|
return func(nick string) string {
|
|
|
|
newNick := nick + "_"
|
2017-07-02 23:07:55 +00:00
|
|
|
|
|
|
|
if newNick == i.GetNick() {
|
2018-05-31 21:24:59 +00:00
|
|
|
state.sendJSON("nick_fail", NickFail{
|
2020-06-15 08:58:51 +00:00
|
|
|
Network: i.Host(),
|
2017-07-02 23:07:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-25 10:02:31 +00:00
|
|
|
state.sendJSON("error", IRCError{
|
2020-06-15 08:58:51 +00:00
|
|
|
Network: i.Host(),
|
2020-05-23 00:30:48 +00:00
|
|
|
Message: fmt.Sprintf("Nickname %s is unavailable, trying %s instead", nick, newNick),
|
2019-01-25 10:02:31 +00:00
|
|
|
})
|
2017-04-06 21:26:58 +00:00
|
|
|
|
|
|
|
return newNick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
func connectIRC(network *storage.Network, state *State, srcIP []byte) *irc.Client {
|
2018-12-11 09:51:20 +00:00
|
|
|
cfg := state.srv.Config()
|
2020-06-15 23:22:23 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
ircCfg := network.IRCConfig()
|
2020-06-15 23:22:23 +00:00
|
|
|
ircCfg.AutoCTCP = cfg.AutoCTCP
|
2018-12-11 09:51:20 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
if ircCfg.TLS {
|
2020-05-23 07:42:20 +00:00
|
|
|
ircCfg.TLSConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: !cfg.VerifyCertificates,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cert := state.user.GetCertificate(); cert != nil {
|
|
|
|
ircCfg.TLSConfig.Certificates = []tls.Certificate{*cert}
|
|
|
|
}
|
2018-05-18 01:14:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 07:42:20 +00:00
|
|
|
if cfg.HexIP {
|
|
|
|
ircCfg.Username = hex.EncodeToString(srcIP)
|
2020-05-23 06:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
if ircCfg.ServerPassword == "" &&
|
2020-05-23 07:42:20 +00:00
|
|
|
cfg.Defaults.ServerPassword != "" &&
|
2020-06-15 08:58:51 +00:00
|
|
|
ircCfg.Host == cfg.Defaults.Host {
|
2020-06-04 00:28:41 +00:00
|
|
|
ircCfg.ServerPassword = cfg.Defaults.ServerPassword
|
2017-07-02 01:31:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 01:04:27 +00:00
|
|
|
if cfg.Proxy.Enabled && strings.ToLower(cfg.Proxy.Protocol) == "socks5" {
|
|
|
|
addr := net.JoinHostPort(cfg.Proxy.Host, cfg.Proxy.Port)
|
|
|
|
|
|
|
|
var auth *proxy.Auth
|
|
|
|
if cfg.Proxy.Username != "" {
|
|
|
|
auth = &proxy.Auth{
|
|
|
|
User: cfg.Proxy.Username,
|
|
|
|
Password: cfg.Proxy.Password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer, err := proxy.SOCKS5("tcp", addr, auth, irc.DefaultDialer)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
ircCfg.Dialer = dialer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 02:28:34 +00:00
|
|
|
if cfg.Proxy.Enabled && strings.ToLower(cfg.Proxy.Protocol) == "i2p" {
|
|
|
|
addr := net.JoinHostPort(cfg.Proxy.Host, cfg.Proxy.Port)
|
2020-12-07 06:46:53 +00:00
|
|
|
client, err := goSam.NewClientFromOptions(
|
|
|
|
goSam.SetAddr(addr),
|
|
|
|
goSam.SetUnpublished(false),
|
|
|
|
goSam.SetInQuantity(3),
|
|
|
|
goSam.SetOutQuantity(3),
|
|
|
|
goSam.SetInBackups(2),
|
|
|
|
goSam.SetOutBackups(2),
|
2020-12-07 17:14:46 +00:00
|
|
|
goSam.SetInLength(3),
|
|
|
|
goSam.SetOutLength(3),
|
2020-12-07 06:46:53 +00:00
|
|
|
goSam.SetInVariance(-1),
|
|
|
|
goSam.SetOutVariance(-1),
|
|
|
|
goSam.SetCloseIdle(false),
|
|
|
|
goSam.SetDebug(false),
|
|
|
|
goSam.SetLeaseSetEncType("4"),
|
2020-12-07 06:50:07 +00:00
|
|
|
goSam.SetSAMMinVersion(0),
|
|
|
|
goSam.SetSAMMaxVersion(1),
|
2020-12-07 06:46:53 +00:00
|
|
|
)
|
|
|
|
//client, err := goSam.NewClient(addr)
|
2020-07-30 02:28:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
2020-07-30 03:04:13 +00:00
|
|
|
ircCfg.Dialer = client
|
2020-07-30 02:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
i := irc.NewClient(ircCfg)
|
2020-05-23 07:42:20 +00:00
|
|
|
i.Config.HandleNickInUse = createNickInUseHandler(i, state)
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
state.setNetwork(network.Host, state.user.NewNetwork(network, i))
|
2020-05-23 07:42:20 +00:00
|
|
|
i.Connect()
|
2018-05-31 21:24:59 +00:00
|
|
|
go newIRCHandler(i, state).run()
|
2017-07-02 01:31:00 +00:00
|
|
|
|
|
|
|
return i
|
|
|
|
}
|