forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winsock.go
140 lines (123 loc) · 3.44 KB
/
winsock.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
// +build windows
package main
import (
"log"
"syscall"
"unsafe"
ole "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/go-ole/go-ole"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/go-ole/go-ole/oleutil"
)
type EventReceiver struct {
lpVtbl *EventReceiverVtbl
ref int32
host *ole.IDispatch
}
type EventReceiverVtbl struct {
pQueryInterface uintptr
pAddRef uintptr
pRelease uintptr
pGetTypeInfoCount uintptr
pGetTypeInfo uintptr
pGetIDsOfNames uintptr
pInvoke uintptr
}
func QueryInterface(this *ole.IUnknown, iid *ole.GUID, punk **ole.IUnknown) uint32 {
s, _ := ole.StringFromCLSID(iid)
*punk = nil
if ole.IsEqualGUID(iid, ole.IID_IUnknown) ||
ole.IsEqualGUID(iid, ole.IID_IDispatch) {
AddRef(this)
*punk = this
return ole.S_OK
}
if s == "{248DD893-BB45-11CF-9ABC-0080C7E7B78D}" {
AddRef(this)
*punk = this
return ole.S_OK
}
return ole.E_NOINTERFACE
}
func AddRef(this *ole.IUnknown) int32 {
pthis := (*EventReceiver)(unsafe.Pointer(this))
pthis.ref++
return pthis.ref
}
func Release(this *ole.IUnknown) int32 {
pthis := (*EventReceiver)(unsafe.Pointer(this))
pthis.ref--
return pthis.ref
}
func GetIDsOfNames(this *ole.IUnknown, iid *ole.GUID, wnames []*uint16, namelen int, lcid int, pdisp []int32) uintptr {
for n := 0; n < namelen; n++ {
pdisp[n] = int32(n)
}
return uintptr(ole.S_OK)
}
func GetTypeInfoCount(pcount *int) uintptr {
if pcount != nil {
*pcount = 0
}
return uintptr(ole.S_OK)
}
func GetTypeInfo(ptypeif *uintptr) uintptr {
return uintptr(ole.E_NOTIMPL)
}
func Invoke(this *ole.IDispatch, dispid int, riid *ole.GUID, lcid int, flags int16, dispparams *ole.DISPPARAMS, result *ole.VARIANT, pexcepinfo *ole.EXCEPINFO, nerr *uint) uintptr {
switch dispid {
case 0:
log.Println("DataArrival")
winsock := (*EventReceiver)(unsafe.Pointer(this)).host
var data ole.VARIANT
ole.VariantInit(&data)
oleutil.CallMethod(winsock, "GetData", &data)
s := string(data.ToArray().ToByteArray())
println()
println(s)
println()
case 1:
log.Println("Connected")
winsock := (*EventReceiver)(unsafe.Pointer(this)).host
oleutil.CallMethod(winsock, "SendData", "GET / HTTP/1.0\r\n\r\n")
case 3:
log.Println("SendProgress")
case 4:
log.Println("SendComplete")
case 5:
log.Println("Close")
this.Release()
case 6:
log.Fatal("Error")
default:
log.Println(dispid)
}
return ole.E_NOTIMPL
}
func main() {
ole.CoInitialize(0)
unknown, err := oleutil.CreateObject("{248DD896-BB45-11CF-9ABC-0080C7E7B78D}")
if err != nil {
panic(err.Error())
}
winsock, _ := unknown.QueryInterface(ole.IID_IDispatch)
iid, _ := ole.CLSIDFromString("{248DD893-BB45-11CF-9ABC-0080C7E7B78D}")
dest := &EventReceiver{}
dest.lpVtbl = &EventReceiverVtbl{}
dest.lpVtbl.pQueryInterface = syscall.NewCallback(QueryInterface)
dest.lpVtbl.pAddRef = syscall.NewCallback(AddRef)
dest.lpVtbl.pRelease = syscall.NewCallback(Release)
dest.lpVtbl.pGetTypeInfoCount = syscall.NewCallback(GetTypeInfoCount)
dest.lpVtbl.pGetTypeInfo = syscall.NewCallback(GetTypeInfo)
dest.lpVtbl.pGetIDsOfNames = syscall.NewCallback(GetIDsOfNames)
dest.lpVtbl.pInvoke = syscall.NewCallback(Invoke)
dest.host = winsock
oleutil.ConnectObject(winsock, iid, (*ole.IUnknown)(unsafe.Pointer(dest)))
_, err = oleutil.CallMethod(winsock, "Connect", "127.0.0.1", 80)
if err != nil {
log.Fatal(err)
}
var m ole.Msg
for dest.ref != 0 {
ole.GetMessage(&m, 0, 0, 0)
ole.DispatchMessage(&m)
}
}