2020-06-13 10:43:47 -04:00
|
|
|
package params
|
|
|
|
|
|
|
|
|
|
import (
|
2020-11-19 08:50:55 -05:00
|
|
|
"encoding/pem"
|
|
|
|
|
"fmt"
|
2020-12-29 00:55:31 +00:00
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2020-07-14 00:17:31 +00:00
|
|
|
"strings"
|
|
|
|
|
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2020-06-13 10:43:47 -04:00
|
|
|
libparams "github.com/qdm12/golibs/params"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetCyberghostGroup obtains the server group for the Cyberghost server from the
|
2020-10-20 02:45:28 +00:00
|
|
|
// environment variable CYBERGHOST_GROUP.
|
2020-06-13 14:08:29 -04:00
|
|
|
func (p *reader) GetCyberghostGroup() (group string, err error) {
|
2020-10-20 02:45:28 +00:00
|
|
|
s, err := p.envParams.GetValueIfInside("CYBERGHOST_GROUP",
|
|
|
|
|
constants.CyberghostGroupChoices(), libparams.Default("Premium UDP Europe"))
|
2020-06-13 14:08:29 -04:00
|
|
|
return s, err
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 17:15:42 -04:00
|
|
|
// GetCyberghostRegions obtains the country names for the Cyberghost servers from the
|
2020-10-20 02:45:28 +00:00
|
|
|
// environment variable REGION.
|
2020-10-18 17:15:42 -04:00
|
|
|
func (p *reader) GetCyberghostRegions() (regions []string, err error) {
|
2020-10-24 19:09:54 +00:00
|
|
|
return p.envParams.GetCSVInPossibilities("REGION", constants.CyberghostRegionChoices())
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCyberghostClientKey obtains the one line client key to use for openvpn from the
|
2020-12-29 19:54:58 +00:00
|
|
|
// file at /gluetun/client.key.
|
2020-06-13 10:43:47 -04:00
|
|
|
func (p *reader) GetCyberghostClientKey() (clientKey string, err error) {
|
2020-12-29 00:55:31 +00:00
|
|
|
const filepath = string(constants.ClientKey)
|
|
|
|
|
file, err := p.os.OpenFile(filepath, os.O_RDONLY, 0)
|
2020-07-14 00:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2020-12-29 00:55:31 +00:00
|
|
|
content, err := ioutil.ReadAll(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = file.Close()
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2020-11-19 08:50:55 -05:00
|
|
|
return extractClientKey(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func extractClientKey(b []byte) (key string, err error) {
|
|
|
|
|
pemBlock, _ := pem.Decode(b)
|
|
|
|
|
if pemBlock == nil {
|
|
|
|
|
return "", fmt.Errorf("cannot decode PEM block from client key")
|
|
|
|
|
}
|
|
|
|
|
parsedBytes := pem.EncodeToMemory(pemBlock)
|
|
|
|
|
s := string(parsedBytes)
|
|
|
|
|
s = strings.ReplaceAll(s, "\n", "")
|
|
|
|
|
s = strings.TrimPrefix(s, "-----BEGIN PRIVATE KEY-----")
|
|
|
|
|
s = strings.TrimSuffix(s, "-----END PRIVATE KEY-----")
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCyberghostClientCertificate obtains the client certificate to use for openvpn from the
|
|
|
|
|
// file at /gluetun/client.crt.
|
|
|
|
|
func (p *reader) GetCyberghostClientCertificate() (clientCertificate string, err error) {
|
2020-12-29 00:55:31 +00:00
|
|
|
const filepath = string(constants.ClientCertificate)
|
|
|
|
|
file, err := p.os.OpenFile(filepath, os.O_RDONLY, 0)
|
2020-11-19 08:50:55 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2020-12-29 00:55:31 +00:00
|
|
|
content, err := ioutil.ReadAll(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = file.Close()
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2020-11-19 08:50:55 -05:00
|
|
|
return extractClientCertificate(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func extractClientCertificate(b []byte) (certificate string, err error) {
|
|
|
|
|
pemBlock, _ := pem.Decode(b)
|
|
|
|
|
if pemBlock == nil {
|
|
|
|
|
return "", fmt.Errorf("cannot decode PEM block from client certificate")
|
|
|
|
|
}
|
|
|
|
|
parsedBytes := pem.EncodeToMemory(pemBlock)
|
|
|
|
|
s := string(parsedBytes)
|
2020-07-14 00:17:31 +00:00
|
|
|
s = strings.ReplaceAll(s, "\n", "")
|
2020-11-19 08:50:55 -05:00
|
|
|
s = strings.TrimPrefix(s, "-----BEGIN CERTIFICATE-----")
|
|
|
|
|
s = strings.TrimSuffix(s, "-----END CERTIFICATE-----")
|
2020-07-14 00:17:31 +00:00
|
|
|
return s, nil
|
2020-06-13 10:43:47 -04:00
|
|
|
}
|