package bootstrap

import (
	"regexp"
)

type configVars struct {
	CA        string
	Cert      string
	Key       string
	IPAddr    string
	Port      string
	Transport string
}

// TODO: sanitize the options coming from the service and add them here too
// instead of hardcoding (cipher, etc)
var openvpnConfigTemplate = `# openvpn config generated by bitmaskcfg
client
dev tun
verb 3
tls-client
remote-cert-tls server
tls-version-min 1.2
tls-cipher DHE-RSA-AES128-SHA
cipher AES-128-CBC
pull-filter ignore ifconfig-ipv6
pull-filter ignore route-ipv6
proto {{ .Transport }}
remote {{ .IPAddr }} {{ .Port }} {{ .Transport }}
<ca>
{{ .CA }}
</ca>
<cert>
{{ .Cert }}
</cert>
<key>
{{ .Key }}
</key>`

var (
	rsaBegin  = "-----BEGIN RSA PRIVATE KEY-----"
	rsaEnd    = "-----END RSA PRIVATE KEY-----"
	keyBegin  = "-----BEGIN PRIVATE KEY-----"
	keyEnd    = "-----END PRIVATE KEY-----"
	certBegin = "-----BEGIN CERTIFICATE-----"
	certEnd   = "-----END CERTIFICATE-----"
)

func matchDelimitedString(str, left, right string) string {
	rx := regexp.MustCompile(`(?s)` + regexp.QuoteMeta(left) + `(.*?)` + regexp.QuoteMeta(right))
	matches := rx.FindAllStringSubmatch(str, -1)
	if len(matches) != 1 {
		return ""
	}
	return left + matches[0][1] + right
}

// TODO get the CA from the service too
var riseupCA = `-----BEGIN CERTIFICATE-----
MIIFjTCCA3WgAwIBAgIBATANBgkqhkiG9w0BAQ0FADBZMRgwFgYDVQQKDA9SaXNl
dXAgTmV0d29ya3MxGzAZBgNVBAsMEmh0dHBzOi8vcmlzZXVwLm5ldDEgMB4GA1UE
AwwXUmlzZXVwIE5ldHdvcmtzIFJvb3QgQ0EwHhcNMTQwNDI4MDAwMDAwWhcNMjQw
NDI4MDAwMDAwWjBZMRgwFgYDVQQKDA9SaXNldXAgTmV0d29ya3MxGzAZBgNVBAsM
Emh0dHBzOi8vcmlzZXVwLm5ldDEgMB4GA1UEAwwXUmlzZXVwIE5ldHdvcmtzIFJv
b3QgQ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