Skip to content

chore(core): add configuration to the forward proxy server #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package core

import (
"fmt"
"log"
"os"

"gopkg.in/yaml.v3"
)

type ServerAddress struct {
HostName string `yaml:"host"`
Port string `yaml:"port"`
}

type TLS struct {
CrtPath string `yaml:"crt"`
Key string `yaml:"key"`
}

type ConnectionCredentials struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Url string `yaml:"url"`
}

type Config struct {
ServerAddress ServerAddress `yaml:"server"`
Tls *TLS `yaml:"tls,omitempty"`
AccessLogging *ConnectionCredentials `yaml:"logging_server,omitempty"`
}

// fileExists checks if a file exists and is not a directory before we try using it to prevent further errors.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

// LoadConfigFromFile loads the Config from the given path
func LoadConfigFromFile(path string) (Config, error) {
if fileExists(path) {
content, err := os.ReadFile(path)
if err != nil {
log.Printf("Error while reading file %s: %v", path, err)
return Config{}, err
}
var config Config
err = yaml.Unmarshal(content, &config)
if err != nil {
log.Printf("Error parsing YAML file %s: %v", path, err)
}
return config, nil
}
return Config{}, fmt.Errorf("%s does not exists", path)
}
13 changes: 11 additions & 2 deletions core/fp.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,17 @@ func getSNI(reader *bufio.Reader) (string, error) {
return hostname, nil
}

func StartForwardProxy(hostname, port string) {
address := fmt.Sprintf("%s:%s", hostname, port)
func Start(config Config) {
if config.Tls == nil {
log.Println("Starting Forward Proxy Server over HTTP")
startForwardProxyHTTP(config)
return
}

}

func startForwardProxyHTTP(config Config) {
address := fmt.Sprintf("%s:%s", config.ServerAddress.HostName, config.ServerAddress.Port)
log.Printf("Forward proxy address: %s", address)
listener, err := net.Listen("tcp", address)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions core/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/Makepad-fr/sfp/core

go 1.22

require gopkg.in/yaml.v3 v3.0.1
3 changes: 3 additions & 0 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29 changes: 28 additions & 1 deletion forward-proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"log"
"os"

"github.com/Makepad-fr/sfp/core"
)

var hostname = ""
var port = "9090"
var config core.Config

func init() {
val, ok := os.LookupEnv("SFP_HOSTNAME")
Expand All @@ -18,8 +20,33 @@ func init() {
if ok {
port = val
}
var err error
if len(os.Args) > 1 {
config, err = core.LoadConfigFromFile(os.Args[1])
if err != nil {
log.Fatalf("Error while loading configuration file %s: %v", os.Args[1], err)
}
return
}
val, ok = os.LookupEnv("SFP_CONFIG_FILE")
if ok {
config, err = core.LoadConfigFromFile(val)
if err != nil {
log.Fatalf("Error while loading configuration file %s: %v", val, err)
}
return
}
config = core.Config{
Tls: nil,
ServerAddress: core.ServerAddress{
HostName: hostname,
Port: port,
},
AccessLogging: nil,
}

}

func main() {
core.StartForwardProxy(hostname, port)
core.Start(config)
}
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=