Skip to content

Commit 2d5d14e

Browse files
authored
Create HFP
1 parent 0e06a47 commit 2d5d14e

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

HFP

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io"
7+
"log"
8+
"net"
9+
"os"
10+
"time"
11+
"io/ioutil"
12+
)
13+
14+
var localAddr *string = flag.String("l", ":9060", "local address")
15+
var remoteAddr *string = flag.String("r", "192.168.2.2:9060", "remote address")
16+
17+
func fileReplay(conn *net.TCPConn, file *os.File) {
18+
19+
defer file.Close()
20+
f, errreplay := io.Copy(conn, file)
21+
if errreplay != nil {
22+
fmt.Println("Replaying file to backend HEP error\n", errreplay)
23+
return
24+
}
25+
log.Println("-->||File successfully replayed to backend HEP", f)
26+
27+
}
28+
29+
func proxyConn(conn *net.TCPConn) {
30+
31+
outFile, err := os.OpenFile("HEP/HEP-saved.arch", os.O_RDWR, 0664)
32+
if err != nil {
33+
fmt.Println("Open HEP file error", err)
34+
return
35+
}
36+
37+
38+
buf := make([]byte, 1024*8)
39+
40+
rConn, err := net.DialTimeout("tcp4", "10.116.118.51:9060", 5*time.Second)
41+
42+
if err != nil {
43+
log.Println("||-->X Dial OUT error", err)
44+
defer outFile.Close()
45+
go func () { _, errcopyfile := io.Copy(outFile, conn)
46+
if errcopyfile != nil {
47+
fmt.Println("Copy to FILE error\n", errcopyfile)
48+
return
49+
} }()
50+
//HEPsave()
51+
log.Printf("-->||Receiving HEP to LOG")
52+
53+
for range time.Tick(time.Second * 10) {
54+
conn, err_outreconn := net.DialTimeout("tcp4", *remoteAddr, 5*time.Second)
55+
if err_outreconn == nil {
56+
log.Println("||-->V Dial OUT reconnected", err_outreconn)
57+
break
58+
}
59+
log.Println("||-->X Dial OUT reconnect failure - retrying", conn)
60+
}
61+
return
62+
} else {
63+
log.Println("||--> Connected OUT", rConn.RemoteAddr())
64+
65+
66+
HEPFileData, HEPFileDataerr := ioutil.ReadFile("HEP/HEP-saved.arch")
67+
if HEPFileDataerr != nil {
68+
fmt.Println("Read HEP file error", err)
69+
return
70+
}
71+
72+
n, err := rConn.Write(HEPFileData)
73+
if err != nil {
74+
log.Println("||-->X Send HEP from LOG error", err)
75+
return
76+
}else{
77+
log.Println("||-->V Send HEP from LOG OK -", n, "bytes")
78+
log.Println("Flushing HEP file")
79+
os.Create("HEP/HEP-saved.arch")
80+
}
81+
82+
}
83+
84+
defer rConn.Close()
85+
86+
for {
87+
data, err_inconn := conn.Read(buf)
88+
if err_inconn != nil {
89+
log.Println("-->X||Read IN packets error:", err_inconn)
90+
if err_inconn != io.EOF {
91+
log.Println("-->X||Read IN packets error:", err_inconn)
92+
}
93+
break
94+
}
95+
log.Printf("-->||Received:\n%q", string(buf[:data]))
96+
log.Println("-->|| Got", data, "bytes")
97+
log.Println("-->|| Total buffer size:", len(buf))
98+
99+
if _, err_HEPout := fmt.Fprint(rConn, string(buf[:data])); err_HEPout != nil {
100+
log.Println("||--> Sending HEP OUT error:", err)
101+
log.Println("-->||Received HEP to LOG", buf[:data])
102+
break
103+
}
104+
log.Println("||--> Sending HEP OUT successful", rConn.RemoteAddr())
105+
106+
}
107+
108+
data := make([]byte, 1024*8)
109+
n, err := rConn.Read(data)
110+
if err != nil {
111+
if err != io.EOF {
112+
log.Println("||<-- Received:", err)
113+
return
114+
} else {
115+
log.Println("||<-- Received:", err, data[:n])
116+
}
117+
}
118+
119+
}
120+
121+
func handleConn(in <-chan *net.TCPConn, out chan<- *net.TCPConn) {
122+
for conn := range in {
123+
proxyConn(conn)
124+
out <- conn
125+
}
126+
}
127+
128+
func closeConn(in <-chan *net.TCPConn) {
129+
for conn := range in {
130+
conn.Close()
131+
}
132+
}
133+
134+
func main() {
135+
flag.Parse()
136+
137+
errmkdir := os.Mkdir("HEP", 0755)
138+
if errmkdir != nil {
139+
log.Println(errmkdir)
140+
}
141+
142+
_, err := os.Create("HEP/HEP-saved.arch")
143+
if err != nil {
144+
fmt.Println("Create file error", err)
145+
return
146+
}
147+
148+
fmt.Printf("Listening: %v\nProxying: %v\n\n", *localAddr, *remoteAddr)
149+
150+
addr, err := net.ResolveTCPAddr("tcp", *localAddr)
151+
if err != nil {
152+
log.Println(err)
153+
return
154+
}
155+
156+
listener, err := net.ListenTCP("tcp4", addr)
157+
if err != nil {
158+
fmt.Println("X|| Server starting error", err)
159+
os.Exit(1)
160+
}
161+
162+
pending, complete := make(chan *net.TCPConn), make(chan *net.TCPConn)
163+
164+
for i := 1; i <= 4; i++ {
165+
go handleConn(pending, complete)
166+
}
167+
go closeConn(complete)
168+
169+
for {
170+
conn, err := listener.AcceptTCP()
171+
log.Println("New connection from", conn.RemoteAddr())
172+
if err != nil {
173+
log.Println(err)
174+
return
175+
}
176+
pending <- conn
177+
}
178+
}

0 commit comments

Comments
 (0)