2021-08-18 20:14:02 +00:00
|
|
|
package custom
|
2021-03-13 08:51:05 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-18 20:12:26 +00:00
|
|
|
var (
|
2021-08-18 20:18:49 +00:00
|
|
|
ErrReadCustomConfig = errors.New("cannot read custom configuration file")
|
|
|
|
|
ErrExtractConnection = errors.New("cannot extract connection from custom configuration file")
|
2021-08-18 20:12:26 +00:00
|
|
|
)
|
2021-03-13 08:51:05 -05:00
|
|
|
|
2021-08-18 20:18:49 +00:00
|
|
|
func BuildConfig(settings configuration.OpenVPN) (
|
2021-03-13 08:51:05 -05:00
|
|
|
lines []string, connection models.OpenVPNConnection, err error) {
|
2021-07-23 16:06:19 +00:00
|
|
|
lines, err = readCustomConfigLines(settings.Config)
|
2021-03-13 08:51:05 -05:00
|
|
|
if err != nil {
|
2021-08-18 20:18:49 +00:00
|
|
|
return nil, connection, fmt.Errorf("%w: %s", ErrReadCustomConfig, err)
|
2021-03-13 08:51:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection, err = extractConnectionFromLines(lines)
|
|
|
|
|
if err != nil {
|
2021-08-18 20:18:49 +00:00
|
|
|
return nil, connection, fmt.Errorf("%w: %s", ErrExtractConnection, err)
|
2021-03-13 08:51:05 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 20:12:26 +00:00
|
|
|
lines = modifyCustomConfig(lines, settings, connection)
|
|
|
|
|
|
2021-03-13 08:51:05 -05:00
|
|
|
return lines, connection, nil
|
|
|
|
|
}
|