From 2494a21d4fa993919b4b4a434cab2e9476d4f164 Mon Sep 17 00:00:00 2001
From: vaninpetr <>
Date: Sat, 7 Jan 2023 00:39:40 +0300
Subject: [PATCH] initial commit
---
.idea/.gitignore | 8 +
.idea/HttpsBalancer.iml | 9 +
.idea/inspectionProfiles/Project_Default.xml | 20 +++
.idea/misc.xml | 6 +
.idea/modules.xml | 8 +
HttpsBalancerConfig.yml | 13 ++
README.md | 35 ++++
src/main.go | 165 +++++++++++++++++++
8 files changed, 264 insertions(+)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/HttpsBalancer.iml
create mode 100644 .idea/inspectionProfiles/Project_Default.xml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 HttpsBalancerConfig.yml
create mode 100644 README.md
create mode 100644 src/main.go
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..73f69e0
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/.idea/HttpsBalancer.iml b/.idea/HttpsBalancer.iml
new file mode 100644
index 0000000..5e764c4
--- /dev/null
+++ b/.idea/HttpsBalancer.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..a4f4159
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..28a804d
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..817e5d5
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/HttpsBalancerConfig.yml b/HttpsBalancerConfig.yml
new file mode 100644
index 0000000..f71498d
--- /dev/null
+++ b/HttpsBalancerConfig.yml
@@ -0,0 +1,13 @@
+listeners:
+- listenaddress: 0.0.0.0:1443
+ protocol: tcp
+ rules:
+ - host: test0.yourdomain.com
+ ip: 10.1.1.115:1443
+ defaultip: 127.0.0.1:443
+- listenaddress: 0.0.0.0:1080
+ protocol: tcp
+ rules:
+ - host: test3.yourdomain.com
+ ip: 10.1.1.115:80
+ defaultip: 127.0.0.1:80
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..99ee63c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,35 @@
+# Regela HTTPS Balancer
+
+### Lightweght SSL redirector
+### Written by Elizar [@Regela](https://sources.krechet.tech/Regela)
+
+#### Example .yml config included
+#### Usage:
+```
+~> ./HttpsBalancer/main --help
+Usage of ./HttpsBalancer/main:
+ -example
+ -i string
+ /path/to/config/file.yml (default "none")
+
+~> ./HttpsBalancer/main -example
+listeners:
+- listenaddress: 0.0.0.0:1443
+ protocol: tcp
+ packettype: https
+ rules:
+ - host: host1.ru
+ ip: 10.10.0.50:443
+ - host: host2.ru
+ ip: 10.10.0.51:443
+ defaultip: 127.0.0.1:443
+- listenaddress: 0.0.0.0:1080
+ protocol: tcp
+ packettype: http
+ rules:
+ - host: host1.ru
+ ip: 10.10.0.50:80
+ - host: host2.ru
+ ip: 10.10.0.51:80
+ defaultip: 127.0.0.1:80
+```
\ No newline at end of file
diff --git a/src/main.go b/src/main.go
new file mode 100644
index 0000000..971ea35
--- /dev/null
+++ b/src/main.go
@@ -0,0 +1,165 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "github.com/goccy/go-yaml"
+ "io/ioutil"
+ "log"
+ "net"
+ "os"
+ "strings"
+ "time"
+)
+
+type Rule struct{
+ Host string
+ Ip string
+}
+
+type Listener struct {
+ ListenAddress string
+ Protocol string
+ PacketType string
+ Rules []Rule
+ DefaultIp string
+}
+
+type Settings struct{
+ Listeners []Listener
+}
+
+func printExample() {
+ settings := Settings{
+ Listeners:[]Listener{
+ {
+ ListenAddress: "0.0.0.0:1443",
+ Protocol: "tcp",
+ PacketType: "https",
+ Rules: []Rule{
+ {
+ Host: "host1.ru",
+ Ip: "10.10.0.50:443",
+ },
+ {
+ Host: "host2.ru",
+ Ip: "10.10.0.51:443",
+ },
+ },
+ DefaultIp: "127.0.0.1:443",
+ },
+ {
+ ListenAddress: "0.0.0.0:1080",
+ Protocol: "tcp",
+ PacketType: "http",
+ Rules: []Rule{
+ {
+ Host: "host1.ru",
+ Ip: "10.10.0.50:80",
+ },
+ {
+ Host: "host2.ru",
+ Ip: "10.10.0.51:80",
+ },
+ },
+ DefaultIp: "127.0.0.1:80",
+ },
+ },
+ }
+ out, _ := yaml.Marshal(settings)
+ fmt.Printf("%s",out)
+}
+var settings Settings
+func main(){
+ printExampleFlag := flag.Bool("example",false,"")
+ ConfigFile := flag.String("i", "none", "/path/to/config/file.yml")
+ flag.Parse()
+
+ if *printExampleFlag {
+ printExample()
+ os.Exit(0)
+ }
+
+ cf, err := ioutil.ReadFile(*ConfigFile)
+ if err != nil {
+ log.Fatalf("Read configuration error: %s", err.Error())
+ }
+
+ err = yaml.Unmarshal(cf,&settings)
+ for _, listener := range settings.Listeners{
+ go listen(listener)
+ }
+ for {
+ time.Sleep(time.Hour)
+ }
+}
+
+func listen(listener Listener){
+ log.Println(listener.Protocol, listener.ListenAddress)
+ ln, err := net.Listen(listener.Protocol, listener.ListenAddress)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer ln.Close()
+
+ for {
+ conn, err := ln.Accept()
+ if err != nil {
+ // handle error
+ }
+ go handleConnection(conn, listener)
+ }
+}
+
+func handleConnection(conn net.Conn, listener Listener) {
+ var b = make([]byte, 1024*128)
+ n, err := conn.Read(b)
+ if err != nil{
+ log.Println(err)
+ return
+ }
+ var conn2 net.Conn
+
+ finded := false
+ for _, rule := range listener.Rules{
+ if strings.Contains(string(b),rule.Host) {
+ finded = true
+ conn2, err = net.Dial(listener.Protocol, rule.Ip)
+ fmt.Println(rule.Host)
+ }
+ }
+ if !finded {
+ conn2, err = net.Dial(listener.Protocol, listener.DefaultIp)
+ fmt.Println("Default")
+ }
+ if err != nil{
+ log.Println(err)
+ return
+ }
+ _, err = conn2.Write(b[:n])
+ if err != nil{
+ log.Println(err)
+ return
+ }
+ go connToConn(conn, conn2)
+ go connToConn(conn2, conn)
+}
+
+func connToConn(conn1 net.Conn,conn2 net.Conn){
+ defer conn1.Close()
+ defer conn2.Close()
+
+ var b = make([]byte, 1024*128)
+ for {
+ n, err := conn1.Read(b)
+ if err != nil{
+ log.Println(err.Error())
+ break
+ }
+ _, err = conn2.Write(b[:n])
+ if err != nil{
+ log.Println(err.Error())
+ break
+ }
+ }
+}
\ No newline at end of file