2018-09-13 18:14:22 -04:00
|
|
|
package i2ptunconf
|
|
|
|
|
2021-02-28 14:54:48 -05:00
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-02-28 16:10:55 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/eyedeekay/sam-forwarder/tls"
|
2021-02-28 14:54:48 -05:00
|
|
|
)
|
|
|
|
|
2018-09-13 18:14:22 -04:00
|
|
|
// GetPort443 takes an argument and a default. If the argument differs from the
|
|
|
|
// default, the argument is always returned. If the argument and default are
|
|
|
|
// the same and the key exists, the key is returned. If the key is absent, the
|
|
|
|
// default is returned.
|
|
|
|
func (c *Conf) GetPort443(arg, def string, label ...string) string {
|
|
|
|
if arg != def {
|
|
|
|
return arg
|
|
|
|
}
|
2018-11-29 19:05:19 -05:00
|
|
|
if c.Config == nil {
|
2018-09-13 18:14:22 -04:00
|
|
|
return arg
|
|
|
|
}
|
|
|
|
if x, o := c.Get("targetForPort.443", label...); o {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTargetPort443 sets the port to forward from the config file
|
|
|
|
func (c *Conf) SetTargetPort443(label ...string) {
|
|
|
|
if v, ok := c.Get("targetForPort.443", label...); ok {
|
|
|
|
c.TargetForPort443 = v
|
|
|
|
} else {
|
|
|
|
c.TargetForPort443 = ""
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 14:54:48 -05:00
|
|
|
|
|
|
|
// Get
|
|
|
|
func (c *Conf) GetUseTLS(arg, def bool, label ...string) bool {
|
|
|
|
if arg != def {
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
if c.Config == nil {
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
if x, o := c.GetBool("usetls", label...); o {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAllowZeroHopOut sets the config to allow zero-hop tunnels
|
|
|
|
func (c *Conf) SetUseTLS(label ...string) {
|
|
|
|
if v, ok := c.GetBool("usetls", label...); ok {
|
|
|
|
c.UseTLS = v
|
|
|
|
} else {
|
|
|
|
c.UseTLS = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTLSConfig
|
2021-02-28 16:16:30 -05:00
|
|
|
func (c *Conf) GetTLSConfigCertPem(arg, def string, label ...string) string {
|
2021-02-28 14:54:48 -05:00
|
|
|
if arg != def {
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
if c.Config == nil {
|
|
|
|
return arg
|
|
|
|
}
|
2021-02-28 16:10:55 -05:00
|
|
|
if x, o := c.Get("cert.pem", label...); o {
|
2021-02-28 14:54:48 -05:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClientDest sets the key name from the config file
|
2021-02-28 16:16:30 -05:00
|
|
|
func (c *Conf) SetTLSConfigCertPem(label ...string) {
|
2021-02-28 16:10:55 -05:00
|
|
|
if v, ok := c.Get("cert.pem", label...); ok {
|
2021-02-28 14:54:48 -05:00
|
|
|
c.Cert = v
|
|
|
|
} else {
|
|
|
|
c.Cert = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTLSConfig
|
2021-02-28 16:16:30 -05:00
|
|
|
func (c *Conf) GetTLSConfigKeyPem(arg, def string, label ...string) string {
|
2021-02-28 14:54:48 -05:00
|
|
|
if arg != def {
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
if c.Config == nil {
|
|
|
|
return arg
|
|
|
|
}
|
2021-02-28 16:10:55 -05:00
|
|
|
if x, o := c.Get("key.pem", label...); o {
|
2021-02-28 14:54:48 -05:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClientDest sets the key name from the config file
|
2021-02-28 16:16:30 -05:00
|
|
|
func (c *Conf) SetTLSConfigKeyPem(label ...string) {
|
2021-02-28 16:10:55 -05:00
|
|
|
if v, ok := c.Get("key.pem", label...); ok {
|
2021-02-28 14:54:48 -05:00
|
|
|
c.Pem = v
|
|
|
|
} else {
|
|
|
|
c.Pem = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 16:10:55 -05:00
|
|
|
func (c *Conf) TLSConfig() (*tls.Config, error) {
|
|
|
|
names := []string{c.Base32()}
|
|
|
|
if c.HostName != "" && strings.HasSuffix(c.HostName, ".i2p") {
|
|
|
|
names = append(names, c.HostName)
|
2021-02-28 14:54:48 -05:00
|
|
|
}
|
2021-02-28 16:10:55 -05:00
|
|
|
return i2ptls.TLSConfig(c.Cert, c.Pem, names)
|
2021-02-28 14:54:48 -05:00
|
|
|
}
|