-
Notifications
You must be signed in to change notification settings - Fork 1
/
petulant.go
229 lines (196 loc) · 5.81 KB
/
petulant.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
// Package main runs the petulant-lana server.
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"math"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
)
// Define the types
type configuration struct {
Name string `json:"name"`
Url string `json:"url"`
CallbackSecret string `json:"callbacksecret"`
BasePrice int `json:"baseprice"`
MinimumPrice int `json:"minprice"`
ApiKey string `json:"coinbasekey"`
}
type transactionResult struct {
Success bool `json:"success"`
Button struct {
Code string `json:"code"`
} `json:"button"`
}
type callbackResult struct {
Order struct {
Filename string `json:"custom"`
} `json:"order"`
}
type buttonType struct {
Name string `json:"name"`
Price string `json:"price_string"`
Currency string `json:"price_currency_iso"`
Filename string `json:"custom"`
CallbackUrl string `json:"callback_url"`
Description string `json:"description"`
Type string `json:"type"`
Style string `json:"style"`
}
type coinbaseRequest struct {
Button buttonType `json:"button"`
}
type uploadStruct struct {
Code string
Url string
}
// Create the configuration
var config = configuration{}
func init() {
// Seed the RNG. Only needs doing once at startup.
rand.Seed(time.Now().UTC().UnixNano())
// Open config file.
configFile, err := os.Open("config.json")
if err != nil {
log.Fatal("failed to open config: ", err)
}
defer configFile.Close()
// Decode config.
decoder := json.NewDecoder(configFile)
err = decoder.Decode(&config)
if err != nil {
log.Fatal("failed to decode config: ", err)
}
}
// Get an appropriate name for the file.
func newFileName(fname string) string {
// First, remove slashes and spaces, replace with dashes.
newName := strings.Replace(strings.Replace(fname, "/", "-", -1), " ", "-", -1)
// Does the current file already exist, in storage.
if _, err := os.Stat("f/" + newName); os.IsNotExist(err) {
// Does the current file already exist, in temporary storage.
if _, err := os.Stat("tmp/" + newName); os.IsNotExist(err) {
// Don't do anything.
} else {
// Add a random number onto the front of the filename.
// This is not the best method, but it does for now.
randomLetter := fmt.Sprint(rand.Intn(10))
newName = newFileName(randomLetter + newName)
}
} else {
// Add a random number onto the front of the filename.
// This is not the best method, but it does for now.
randomLetter := fmt.Sprint(rand.Intn(10))
newName = newFileName(randomLetter + newName)
}
return newName
}
// Create a coinbase button.
func createButton(n string, p int) string {
buttonCode := buttonType{
Name: "One-Time Hosting Purchase",
Price: strconv.FormatFloat(float64(p)/float64(100000000), 'f', 8, 64),
Currency: "BTC",
Filename: n,
CallbackUrl: fmt.Sprintf("%s/%s", config.Url, config.CallbackSecret),
Description: fmt.Sprintf("Indefinite storage of the provided file. Your file will be available at: %s/%s when the transaction processes.", config.Url, n),
Type: "buy_now",
Style: "custom_large",
}
coinbaseRequest := coinbaseRequest{Button: buttonCode}
data, err := json.Marshal(coinbaseRequest)
if err != nil {
log.Println("creating button: ", err)
}
request_body := bytes.NewBuffer(data)
client := &http.Client{}
req, err := http.NewRequest("POST", "https://coinbase.com/api/v1/buttons?api_key="+config.ApiKey, request_body)
if err != nil {
log.Println("creating coinbase request: ", err)
}
req.Header.Add("content-type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("completing coinbase request: ", err)
}
defer resp.Body.Close()
res := transactionResult{}
resDecoder := json.NewDecoder(resp.Body)
err = resDecoder.Decode(&res)
if err != nil {
log.Println("decoding coinbase response: ", err)
}
return res.Button.Code
}
// hello world, the web server
func upload(w http.ResponseWriter, req *http.Request) {
// Get the form file.
file, header, err := req.FormFile("file")
if err != nil {
log.Println("form file: ", err)
return
}
// Get the name for the file.
fileName := newFileName(header.Filename)
log.Print("uploaded new file: ", fileName)
dataFile, err := os.Create("tmp/" + fileName)
if err != nil {
log.Println("opening file for writing: ", err)
}
defer dataFile.Close()
io.Copy(dataFile, file)
// Get file size.
fileInfo, _ := os.Stat("tmp/" + fileName)
fileSize := math.Floor(float64(fileInfo.Size()) / 1024)
price := int(math.Floor(float64(config.BasePrice) * (fileSize / 1024)))
if price < config.MinimumPrice {
price = config.MinimumPrice
}
// Create template.
uploadInfo := uploadStruct{
Code: createButton(fileName, price),
Url: fmt.Sprintf("%s/f/%s", config.Url, fileName),
}
t, _ := template.ParseFiles("upload.html")
err = t.Execute(w, uploadInfo)
if err != nil {
log.Println("loading upload page: ", err)
}
}
func coinbaseCallback(w http.ResponseWriter, req *http.Request) {
res := callbackResult{}
decoder := json.NewDecoder(req.Body)
decoder.Decode(&res)
os.Rename("tmp/"+res.Order.Filename, "f/"+res.Order.Filename)
}
func mainPage(w http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("index.html")
err := t.Execute(w, config)
if err != nil {
log.Println("Error loading main page: ", err)
}
}
func main() {
bindAddr := flag.String("port", "8080", "Server port.")
iface := flag.String("iface", "0.0.0.0", "Interface to bind to.")
flag.Parse()
// Main page
http.HandleFunc("/", mainPage)
// Upload page
http.HandleFunc("/upload", upload)
// Coinbase callback
http.HandleFunc("/"+config.CallbackSecret, coinbaseCallback)
// Static files
http.Handle("/f/", http.FileServer(http.Dir("")))
log.Println("Binding to port", *bindAddr)
log.Fatal(http.ListenAndServe(*iface+":"+*bindAddr, nil))
}