Git Product home page Git Product logo

webrouter's People

Contributors

leeli73 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

sclang007

webrouter's Issues

代码不规范,亲人两行泪~

package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"strings"
)

type rule struct {
Domain string json:Domain
Address string json:Address
}
type config struct {
MainPort string json:MainPort
rules []rule json:rules
}

var myconfig config

func initconfig() {
var Data, err = ioutil.ReadFile("config.json")
if err != nil {
log.Println("Read config File Error!")
os.Exit(0)
return
}
err = json.Unmarshal(Data, &myconfig)
if err != nil {
log.Println("Read config JSON Error!Please Check!")
os.Exit(0)
return
}
fmt.Println("Main Port:" + myconfig.MainPort)
for i := 0; i < len(myconfig.rules); i++ {
fmt.Println("Domain: " + myconfig.rules[i].Domain + " <----> " + myconfig.rules[i].Address)
}
}
func handleClientRequest(client net.Conn) {
if client == nil {
return
}
defer client.Close()
var b [1024]byte
n, err := client.Read(b[:])
if err != nil {
log.Println(err)
return
}
var method, url, HTTPv, address string
fmt.Sscanf(string(b[:bytes.IndexByte(b[:], '\n')]), "%s%s%s", &method, &url, &HTTPv)
address = getaddress(string(b[:]))
if address == "nil" {
fmt.Println("Unknow Domain")
return
}
server, err := net.Dial("tcp", address)
if err != nil {
log.Println(err)
return
}
if method == "CONNECT" {
fmt.Fprint(client, "HTTP/1.1 200 Connection established\r\n\r\n")
} else {
server.Write(b[:n])
}
go io.Copy(server, client)
io.Copy(client, server)
}
func getaddress(HTTPData string) string {
Lines := strings.Split(HTTPData, "\r\n")
for i := 0; i < len(Lines); i++ {
line := Lines[i]
temp := strings.Split(line, ": ")
if strings.Compare(temp[0], "Host") == 0 {
for i := 0; i < len(myconfig.rules); i++ {
if strings.Compare(myconfig.rules[i].Domain, temp[1]) == 0 {
return myconfig.rules[i].Address
}
}
}
}
return "nil"
}
func compressStr(str string) string {
if str == "" {
return ""
}
reg := regexp.MustCompile("\s+")
return reg.ReplaceAllString(str, "")
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
initconfig()
l, err := net.Listen("tcp", ":"+myconfig.MainPort)
if err != nil {
log.Panic(err)
}
for {
client, err := l.Accept()
if err != nil {
log.Panic(err)
}
go handleClientRequest(client)
}
}

结构体小写开头
函数名小写开头
复制粘贴后调试时,还能丢失括号。。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.