-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (110 loc) · 4.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bufio"
"bytes"
"encoding/binary"
"flag"
"fmt"
"net"
"os"
"github.com/op/go-logging"
"github.com/timapril/go-registrar/epp"
)
var log = logging.MustGetLogger("registrar")
var format = logging.MustStringFormatter(
"%{color}%{level:.4s} %{time:15:04:05.000000} %{shortfile} %{callpath} %{longfunc} %{id:03x}%{color:reset} %{message}",
)
var (
verbose = flag.Bool("v", false, "Verbose logging")
veryverbose = flag.Bool("vv", false, "Very Verbose logging")
)
// prepareLogging sets up logging so the application can have
// configurable logging
func prepareLogging(level logging.Level) {
backend := logging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
backendLevel := logging.AddModuleLevel(backendFormatter)
backendLevel.SetLevel(level, "")
logging.SetBackend(backendLevel)
}
func main() {
flag.Parse()
ll := logging.ERROR
if *verbose {
ll = logging.INFO
}
if *veryverbose {
ll = logging.DEBUG
}
prepareLogging(ll)
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a port number!")
return
}
PORT := ":" + arguments[1]
l, err := net.Listen("tcp4", PORT)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(c)
}
}
func handleConnection(c net.Conn) {
scanner := bufio.NewScanner(c)
scanner.Split(WireSplit)
msg1 := `<?xml version="1.0" encoding="UTF-8" standalone="no"?><epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"><greeting><svID>EPP Server Stub</svID><svDate>2015-09-22T23:53:39.854Z</svDate><svcMenu><version>1.0</version><lang>en</lang><objURI>http://www.verisign.com/epp/lowbalance-poll-1.0</objURI><objURI>urn:ietf:params:xml:ns:contact-1.0</objURI><objURI>http://www.verisign.com/epp/balance-1.0</objURI><objURI>urn:ietf:params:xml:ns:domain-1.0</objURI><objURI>http://www.verisign.com/epp/registry-1.0</objURI><objURI>http://www.nic.name/epp/nameWatch-1.0</objURI><objURI>http://www.verisign-grs.com/epp/suggestion-1.1</objURI><objURI>http://www.verisign.com/epp/rgp-poll-1.0</objURI><objURI>http://www.nic.name/epp/defReg-1.0</objURI><objURI>urn:ietf:params:xml:ns:host-1.0</objURI><objURI>http://www.nic.name/epp/emailFwd-1.0</objURI><objURI>http://www.verisign.com/epp/whowas-1.0</objURI><svcExtension><extURI>urn:ietf:params:xml:ns:secDNS-1.1</extURI><extURI>http://www.verisign.com/epp/whoisInf-1.0</extURI><extURI>urn:ietf:params:xml:ns:secDNS-1.0</extURI><extURI>http://www.verisign.com/epp/idnLang-1.0</extURI><extURI>http://www.nic.name/epp/persReg-1.0</extURI><extURI>http://www.verisign.com/epp/jobsContact-1.0</extURI><extURI>urn:ietf:params:xml:ns:coa-1.0</extURI><extURI>http://www.verisign-grs.com/epp/namestoreExt-1.1</extURI><extURI>http://www.verisign.com/epp/sync-1.0</extURI><extURI>http://www.verisign.com/epp/premiumdomain-1.0</extURI><extURI>http://www.verisign.com/epp/relatedDomain-1.0</extURI><extURI>urn:ietf:params:xml:ns:launch-1.0</extURI><extURI>urn:ietf:params:xml:ns:rgp-1.0</extURI></svcExtension></svcMenu><dcp><access><all></all></access><statement><purpose><admin></admin><prov></prov></purpose><recipient><ours></ours><public></public></recipient><retention><stated></stated></retention></statement></dcp></greeting></epp>`
msgSize := uint32(len(msg1) + 4)
err := binary.Write(c, binary.BigEndian, &msgSize)
if err != nil {
log.Error(err)
return
}
fmt.Println(msgSize)
_, err = c.Write([]byte(msg1))
if err != nil {
log.Error(err)
return
}
fmt.Println("Wrote Greeting")
fmt.Printf("Serving %s\n", c.RemoteAddr().String())
for scanner.Scan() {
text := scanner.Text()
fmt.Println(text)
res := epp.GetEPPResponseResult("1", "1", 1000, epp.ResponseCode1000)
b, _ := res.EncodeEPP()
_, err = c.Write(b)
if err != nil {
log.Error(err)
return
}
// c.Write([]byte(string("foo")))
}
c.Close()
}
// WireSplit is used by the bufio.Scanner to split epp messages apart
// before they are unmarshalled
func WireSplit(data []byte, atEOF bool) (advance int, token []byte, err error) {
avilDataLen := uint32(len(data))
if avilDataLen >= 4 {
var msgSize uint32
lenBuf := bytes.NewBuffer(data[:4])
err = binary.Read(lenBuf, binary.BigEndian, &msgSize)
if avilDataLen >= msgSize && err == nil {
advance = int(msgSize)
token = data[4:msgSize]
err = nil
return
}
return 0, nil, nil
}
return 0, nil, nil
}