package config import ( "crypto/tls" "fmt" "net" "os" "path/filepath" "runtime" "strconv" "strings" "time" "github.com/D-Modem/webone-go/internal/logx" "github.com/D-Modem/webone-go/internal/rx" "github.com/D-Modem/webone-go/internal/textenc" ) // Converter is a [Converters] line: an external program that converts // content (ImageMagick's convert, ffmpeg ...), with %SRC%, %DEST%, %ARG1%, // %ARG2%, %DESTEXT% and %SRCURL% in its command line. type Converter struct { Executable string CommandLine string UseStdout bool // no %DEST%: the result is on standard output UseStdin bool // no %SRC%: the source goes to standard input SelfDownload bool // %SRCURL%: the program fetches the source itself } // ParseConverter parses a [Converters] line. func ParseConverter(s string) (Converter, error) { i := strings.IndexByte(s, ' ') if i < 0 { return Converter{}, fmt.Errorf("converter line is invalid") } c := Converter{Executable: s[:i], CommandLine: s[i+1:]} c.UseStdout = !strings.Contains(c.CommandLine, "%DEST%") c.UseStdin = !strings.Contains(c.CommandLine, "%SRC%") c.SelfDownload = strings.Contains(c.CommandLine, "%SRCURL%") if c.UseStdin && c.SelfDownload { c.UseStdin = false } return c, nil } // Config is the whole configuration (ConfigFile.cs). type Config struct { // [Server] Port int IP net.IP ForceHttps []string ForceUtf8 []string TextTypes []string OutputEncoding *textenc.Encoding // nil: as is CodepageANSI *textenc.Encoding CodepageASCII *textenc.Encoding CodepageMac *textenc.Encoding CodepageEBCDIC *textenc.Encoding CodepageCulture textenc.Culture Authenticate []string AuthenticateMessage string AuthenticateRealm string OpenForLocalIPs bool HideClientErrors bool SearchInArchive bool HideArchiveRedirect bool ArchiveUrlSuffix string ShortenArchiveErrors bool ArchiveDateLimit int Converters []Converter UserAgent string DefaultHostName string ValidateCertificates bool EditRules []*EditSet TranslitTable [][2]string TemporaryDirectory string DisplayStatusPage string IpBanList []string IpWhiteList []string UrlBlackList []string UrlWhiteList []string HostNames []string UpperProxy string PAC string MultipleHttp2Conns bool RemoteHttpVersion string WebVideoOptions map[string]string WebVideoProfiles map[string][]string AllowHttpCompression bool MimeTypes map[string]string EnableWebFtp bool ShowFileIcons bool Http10Only []string EnableManualConverting bool ContentDirectory string DontPreferHTTPS bool ConnectionTimeout int ActivexGalleryEmul bool ActivexGalleryUrls []string ActivexGalleryCLSIDs map[string]string // DnsServer (Go port): resolve upstream names through this server // ("host:port"), such as the BBS's own DNS, rather than the system's. DnsServer string // [SecureProxy] SslEnable bool SslCertificate string SslPrivateKey string SslProtocols []uint16 // TLS versions for clients SslProtocolsSet bool SslCipherSuites []uint16 SslHashAlgorithm string // MD5, SHA1, SHA256, SHA384, SHA512 SslHashAlgorithmSet bool // given in the configuration SecurityProtocols []uint16 // TLS versions for fetching from servers SslRootSubject string SslRootValidAfter time.Time SslRootValidBefore time.Time SslCertValidBeforeNow int SslCertValidAfterNow int SslSiteCerts string SslSiteCertGenerator string AllowNonHttpsCONNECT bool NonHttpSslServers []string NonHttpConnectRedirect map[string]string ConnectPassThrough []string NonHttpSslDecrypt map[string]string // SslCaDirectory (Go port): use the CA in this directory (ca.crt and // ca.key, as the D-Modem BBS keeps its own); never made or replaced. SslCaDirectory string // SslTrustOwnCA (Go port): also trust the proxy's CA when fetching // from servers (for sites the BBS serves under its own CA). SslTrustOwnCA bool // Vars is [Variables] plus the built-in ones (WOVer, WOSystem, // WOConfigDir, Proxy, ProxyHost, ProxyPort). Vars *Vars // where the configuration came from ConfigFileName string CustomConfig string LoadedFiles []string rawEntries [][2]string // location, line sections []*Section // LogFile/AppendLogFile seen in [Server]; opened by the caller unless // the command line overrides it LogFile string LogFileAppend bool } // DefaultCASubject is the subject of a CA WebOne makes; %number% becomes a // random number (two CAs of one name confuse Firefox). const DefaultCASubject = "CN=WebOne Certificate Authority %number%,OU=This is not really secure connection,O=MITM Proxy,C=SU" // Default PAC script parts. const ( DefaultPACHeader = "function FindProxyForURL(url, host){\n" DefaultPACHttp = "if (url.substring(0, 5) == 'http:')\n{ return 'PROXY %PACProxy%'; }\n" DefaultPACHttps = "if (url.substring(0, 6) == 'https:')\n{ return 'PROXY %PACProxy%'; }\n" DefaultPACFtp = "if (url.substring(0, 4) == 'ftp:')\n{ return 'PROXY %PACProxy%'; }\n" DefaultPACFooter = "} /*WebOne PAC*/ " ) // New is the default configuration (before any file). func New() *Config { wd, _ := os.Getwd() host, _ := os.Hostname() lang := os.Getenv("LC_ALL") if lang == "" { lang = os.Getenv("LANG") } return &Config{ Port: 80, TextTypes: []string{"text/", "javascript"}, CodepageCulture: textenc.CultureFor(lang), AuthenticateMessage: "
Hello! This Web 2.0-to-1.0 proxy server is private. Please reload this page and enter your credentials in browser's pop-up window.
", AuthenticateRealm: "WebOne", UserAgent: "%Original% WebOne/%WOVer%", DefaultHostName: host, ValidateCertificates: true, TemporaryDirectory: wd + string(os.PathSeparator), DisplayStatusPage: "full", MultipleHttp2Conns: true, RemoteHttpVersion: "auto", WebVideoOptions: map[string]string{}, WebVideoProfiles: map[string][]string{}, AllowHttpCompression: true, MimeTypes: map[string]string{}, EnableWebFtp: true, ShowFileIcons: true, EnableManualConverting: true, ContentDirectory: "./html", ConnectionTimeout: 100, ActivexGalleryEmul: true, ActivexGalleryCLSIDs: map[string]string{}, SslEnable: true, SslCertificate: "ssl.crt", SslPrivateKey: "ssl.key", SslHashAlgorithm: "SHA1", SslRootSubject: DefaultCASubject, SslCertValidBeforeNow: -7, SslCertValidAfterNow: 7, AllowNonHttpsCONNECT: true, NonHttpConnectRedirect: map[string]string{}, NonHttpSslDecrypt: map[string]string{}, Vars: NewVars(), } } // ToBool reads a yes/no value: 1/0, y/n, yes/no, on/off, enable/disable, // true/false. Anything else is false (WebOne throws; a warning is logged // by callers that care). func ToBool(s string) bool { switch strings.ToLower(strings.TrimSpace(s)) { case "1", "y", "yes", "on", "enable", "true": return true } return false } // IsBool: s is one of the words ToBool knows. func IsBool(s string) bool { switch strings.ToLower(strings.TrimSpace(s)) { case "1", "y", "yes", "on", "enable", "true", "0", "n", "no", "off", "disable", "false", "": return true } return false } // ParseVersion reads "1.1" or "2". func ParseVersion(s string) (major, minor int, ok bool) { s = strings.TrimSpace(s) parts := strings.Split(s, ".") if len(parts) == 0 || len(parts) > 4 { return 0, 0, false } var err error if major, err = strconv.Atoi(parts[0]); err != nil { return 0, 0, false } if len(parts) > 1 { if minor, err = strconv.Atoi(parts[1]); err != nil { return 0, 0, false } } return major, minor, true } // ParseHTTPVersionRule reads a RemoteHttpVersion / AddRequestHttpVersion // value: "1.1" (exactly), "=1.1", ">1.1", "<2.0" or "auto". func ParseHTTPVersionRule(s string) (string, bool) { if rx.Match(`[\d][\.][\d]`, s) && !rx.Match(`^[=<>]`, s) { return "=" + s, true } if rx.Match(`([=>