2010-11-21 20:07:31 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2010-11-27 14:12:12 +00:00
|
|
|
"net"
|
2010-11-21 20:07:31 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"scanner"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cPort struct {
|
|
|
|
Port int
|
2010-11-27 14:12:12 +00:00
|
|
|
BindIP net.IP // bind to a specific IP for listen port
|
|
|
|
Class string // "server" or "client"
|
2010-11-21 20:07:31 +00:00
|
|
|
|
|
|
|
// Is port a tls.Listener? Does it support compression (no)?
|
|
|
|
SSL, Zip bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var portKeywords = keywordMap{
|
2010-11-27 14:12:12 +00:00
|
|
|
"bind_ip": (*Config).parsePortBindIP,
|
2010-11-21 20:07:31 +00:00
|
|
|
"class": (*Config).parsePortClass,
|
|
|
|
"ssl": (*Config).parsePortSSL,
|
2010-11-27 14:12:12 +00:00
|
|
|
"zip": (*Config).parsePortZip,
|
2010-11-21 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultPort() *cPort {
|
2010-11-27 14:12:12 +00:00
|
|
|
return &cPort{
|
|
|
|
BindIP: nil, Class: "client",
|
|
|
|
SSL: false, Zip: false,
|
|
|
|
}
|
2010-11-21 20:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *cPort) String() string {
|
|
|
|
str := []string{fmt.Sprintf("port %d {", p.Port)}
|
2010-11-27 14:12:12 +00:00
|
|
|
if p.BindIP != nil {
|
|
|
|
str = append(str,
|
|
|
|
fmt.Sprintf("\tbind_ip = \"%s\"", p.BindIP.String()))
|
2010-11-21 20:07:31 +00:00
|
|
|
}
|
|
|
|
str = append(str,
|
|
|
|
fmt.Sprintf("\tclass = \"%s\"", p.Class),
|
|
|
|
fmt.Sprintf("\tssl = %t", p.SSL),
|
|
|
|
fmt.Sprintf("\tzip = %t", p.Zip),
|
|
|
|
"}",
|
|
|
|
)
|
|
|
|
return strings.Join(str, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conf *Config) parsePort() {
|
|
|
|
port := defaultPort()
|
|
|
|
portnum, ok := conf.expectInt()
|
|
|
|
if !ok || portnum > 65535 || portnum < 1024 {
|
|
|
|
conf.parseError("Invalid port '%s'", portnum)
|
|
|
|
port = nil
|
|
|
|
} else {
|
|
|
|
port.Port = portnum
|
|
|
|
conf.Ports[portnum] = port
|
|
|
|
}
|
|
|
|
if conf.scan.Peek() != '\n' {
|
|
|
|
conf.parseKwBlock(port, "port", portKeywords)
|
|
|
|
}
|
|
|
|
fmt.Println(port.String())
|
|
|
|
}
|
|
|
|
|
2010-11-27 14:12:12 +00:00
|
|
|
func (conf *Config) parsePortBindIP(pi interface{}) {
|
|
|
|
port := pi.(*cPort)
|
|
|
|
_, text := conf.next()
|
|
|
|
if ip := net.ParseIP(text); ip != nil {
|
|
|
|
port.BindIP = ip
|
|
|
|
} else {
|
|
|
|
conf.parseError("'%s' is not a valid IP address", text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-21 20:07:31 +00:00
|
|
|
func (conf *Config) parsePortClass(pi interface{}) {
|
|
|
|
port := pi.(*cPort)
|
|
|
|
tok, text := conf.next()
|
|
|
|
if tok == scanner.String && (text == "server" || text == "client") {
|
|
|
|
port.Class = text
|
|
|
|
} else {
|
|
|
|
conf.parseError(
|
|
|
|
"Port class must be \"server\" or \"client\", got '%s'", text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conf *Config) parsePortSSL(pi interface{}) {
|
|
|
|
port := pi.(*cPort)
|
|
|
|
if ssl, ok := conf.expectBool(); ok {
|
|
|
|
port.SSL = ssl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-27 14:12:12 +00:00
|
|
|
func (conf *Config) parsePortZip(pi interface{}) {
|
|
|
|
port := pi.(*cPort)
|
|
|
|
if zip, ok := conf.expectBool(); ok {
|
|
|
|
port.Zip = zip
|
|
|
|
}
|
|
|
|
}
|