-
Notifications
You must be signed in to change notification settings - Fork 13
/
Client Example.go
141 lines (119 loc) · 3.2 KB
/
Client Example.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
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
)
var licenseServer string = "http://127.0.0.1:9347/" //Your license server address
func checkFileExist(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
} else {
return true
}
}
func md5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
func licenseCheck() {
if !checkFileExist("license.dat") {
fmt.Println("License file not found.")
fmt.Print("Would you like to register?: ")
scan := bufio.NewScanner(os.Stdin)
scan.Scan()
if scan.Text() == "yes" || scan.Text() == "Yes" || scan.Text() == "YES" {
fmt.Println("If you do not have a key contact your supplier.")
fmt.Print("Please entor your key: ")
scan := bufio.NewScanner(os.Stdin)
scan.Scan()
key := scan.Text()
name, _ := os.Hostname()
usr, _ := user.Current()
fmt.Println("Key:", key)
ioutil.WriteFile("license.dat", []byte(key), 0600)
fmt.Println("HWID:", md5Hash(name+usr.Username))
fmt.Println("Connecting to license server...")
client := &http.Client{}
data := url.Values{}
data.Set("license", key)
data.Set("hwid", md5Hash(name+usr.Username))
u, _ := url.ParseRequestURI(licenseServer)
urlStr := fmt.Sprintf("%v", u)
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(r)
if err != nil {
fmt.Println("Unable to connect to license server.")
os.Exit(0)
} else {
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode == 200 {
if string(resp_body) != "0" {
if string(resp_body) == "1" {
fmt.Println("License is Expired.")
os.Exit(0)
} else if string(resp_body) == "2" {
fmt.Println("Registered!")
fmt.Println("DO NOT DELETE THE 'license.dat' FILE!")
fmt.Println(" ")
} else {
fmt.Println("Unable to verify to license server.")
os.Exit(0)
}
}
} else {
fmt.Println(resp.StatusCode)
}
}
} else {
os.Exit(0)
}
} else {
name, _ := os.Hostname()
usr, _ := user.Current()
dat, _ := ioutil.ReadFile("license.dat")
key := string(dat)
client := &http.Client{}
data := url.Values{}
data.Set("license", key)
data.Set("hwid", md5Hash(name+usr.Username))
u, _ := url.ParseRequestURI(licenseServer)
urlStr := fmt.Sprintf("%v", u)
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(r)
if err != nil {
fmt.Println("Unable to connect to license server.")
os.Exit(0)
} else {
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode == 200 {
if string(resp_body) != "0" {
if string(resp_body) == "1" {
fmt.Println("License is Expired.")
os.Exit(0)
} else {
fmt.Println("Unable to verify to license server.")
os.Exit(0)
}
}
}
}
}
}
func main() {
licenseCheck()
for {
}
}