initial commit

master
vaninpetr 3 years ago
commit 2494a21d4f
  1. 8
      .idea/.gitignore
  2. 9
      .idea/HttpsBalancer.iml
  3. 20
      .idea/inspectionProfiles/Project_Default.xml
  4. 6
      .idea/misc.xml
  5. 8
      .idea/modules.xml
  6. 13
      HttpsBalancerConfig.yml
  7. 35
      README.md
  8. 165
      src/main.go

8
.idea/.gitignore vendored

@ -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/

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,20 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="GoUnhandledErrorResult" enabled="true" level="WARNING" enabled_by_default="true">
<methods>
<method importPath="hash" receiver="Hash" name="Write" />
<method importPath="strings" receiver="*Builder" name="Write" />
<method importPath="strings" receiver="*Builder" name="WriteByte" />
<method importPath="bytes" receiver="*Buffer" name="WriteRune" />
<method importPath="bytes" receiver="*Buffer" name="Write" />
<method importPath="bytes" receiver="*Buffer" name="WriteString" />
<method importPath="strings" receiver="*Builder" name="WriteString" />
<method importPath="bytes" receiver="*Buffer" name="WriteByte" />
<method importPath="strings" receiver="*Builder" name="WriteRune" />
<method importPath="math/rand" receiver="*Rand" name="Read" />
<method importPath="net" receiver="Conn" name="Close" />
</methods>
</inspection_tool>
</profile>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/HttpsBalancer.iml" filepath="$PROJECT_DIR$/.idea/HttpsBalancer.iml" />
</modules>
</component>
</project>

@ -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

@ -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
```

@ -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
}
}
}
Loading…
Cancel
Save