-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
165 lines (131 loc) · 3.85 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
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
package main
import (
"bytes"
_ "encoding/binary"
"encoding/json"
"fmt"
"image"
"image/color"
"image/png"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
_ "net/url"
_ "os"
"strings"
"syscall"
"time"
"unsafe"
)
func main() {
FreeConsole()
for {
if isScreenshotKeyPressed() {
takeScreenshot()
time.Sleep(2 * time.Second)
}
// Yield hack
time.Sleep(100 * time.Millisecond)
}
}
func isScreenshotKeyPressed() bool {
return (GetAsyncKeyState(VK_SCROLL) & 0x8000) != 0
}
func takeScreenshot() {
hwnd := GetForegroundWindow()
rc := GetWindowRect(hwnd)
hdcScreen := GetScreenDC()
hdcTarget := CreateCompatibleDC(hdcScreen)
hbmp := CreateCompatibleBitmap(hdcScreen, rc.Right-rc.Left, rc.Bottom-rc.Top)
SelectObject(hdcTarget, HGDIOBJ(hbmp))
if !BitBlt(hdcTarget, 0, 0, int32(rc.Right-rc.Left), int32(rc.Bottom-rc.Top), hdcScreen, int32(rc.Left), int32(rc.Top), SRCCPY|CAPTUREBLT) {
fmt.Printf("Error: BitBlt: %d\n", GetLastError())
}
var data = make([]byte, (rc.Right-rc.Left)*4*(rc.Bottom-rc.Top))
// Get color data
GetBitmapBits(hbmp, int32((rc.Right-rc.Left)*4*(rc.Bottom-rc.Top)), unsafe.Pointer(&data[0]))
bmp := BITMAP{}
GetObject(HGDIOBJ(hbmp), unsafe.Sizeof(BITMAP{}), &bmp)
DeleteDC(hdcTarget)
DeleteObject(HGDIOBJ(hbmp))
ReleaseScreenDC(hdcScreen)
bitmap := buildBitmap(bmp, data)
uploadToImgur(bitmap)
}
func uploadToImgur(dataBuffer []byte) {
fmt.Printf("Uploading...")
img := bytes.NewBuffer(dataBuffer)
bodyBuffer := new(bytes.Buffer)
multipartWriter := multipart.NewWriter(bodyBuffer)
// Send the API Key
key, _ := multipartWriter.CreateFormField("key")
key.Write([]byte("0ad2497b4d9ebe9870f71fe6ea18cb3f"))
// Send the file
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="image"; filename="blob"`)
h.Set("Content-Type", "image/png")
partWriter, err := multipartWriter.CreatePart(h)
if err != nil {
fmt.Printf("Create Part: %s\n", err)
}
_, err = io.Copy(partWriter, img)
if err != nil {
fmt.Printf("Copy: %s\n", err)
}
multipartWriter.Close()
req, err := http.NewRequest("POST", "http://api.imgur.com/2/upload.json", bodyBuffer)
if err != nil {
fmt.Printf("Create Req: %s\n", err)
}
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
jsonResp := ImgurUploadResponse{}
dec := json.NewDecoder(bytes.NewBuffer(body))
err = dec.Decode(&jsonResp)
if err != nil {
fmt.Printf("%s", err)
}
// Save the refs
storeToFuriousMist(&jsonResp)
PlaySound("whatever")
hMem := GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, 1024)
ptr := GlobalLock(hMem)
CopyMemory(ptr, unsafe.Pointer(syscall.StringToUTF16Ptr(jsonResp.Upload.Links.Original)), 1023)
GlobalUnlock(hMem)
// copy the url to clipboard
OpenTaskClipboard()
EmptyClipboard()
SetClipboardData(CF_UNICODETEXT, HANDLE(hMem))
CloseClipboard()
}
func storeToFuriousMist(data *ImgurUploadResponse) {
origParts := strings.Split(data.Upload.Links.Original, "/")
thumbParts := strings.Split(data.Upload.Links.Small_Square, "/")
http.PostForm("http://furious-mist-4119.herokuapp.com/uploaded", url.Values{
"hash": {data.Upload.Image.Hash},
"deletehash": {data.Upload.Image.DeleteHash},
"orig": {origParts[cap(origParts)-1]},
"thumb": {thumbParts[cap(thumbParts)-1]},
})
}
func buildBitmap(bmp BITMAP, dataBuffer []byte) []byte {
pngBuffer := image.NewNRGBA(image.Rect(0, 0, int(bmp.Width), int(bmp.Height)))
var ptr int = 0
for y := 0; y < int(bmp.Height); y++ {
for x := 0; x < int(bmp.Width); x++ {
pngBuffer.Set(x, y, color.NRGBA{uint8(dataBuffer[ptr+2]), uint8(dataBuffer[ptr+1]), uint8(dataBuffer[ptr+0]), 255})
ptr += 4
}
}
image := new(bytes.Buffer)
png.Encode(image, pngBuffer)
return image.Bytes()
}