-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfp.go
120 lines (108 loc) · 3.64 KB
/
fp.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
// QCLauncher by syncore <[email protected]> 2017
// https://github.com/syncore/qclauncher
package qclauncher
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/Microsoft/go-winio"
"github.com/syncore/qclauncher/resources"
)
const pipePrefix = "\\\\.\\pipe\\"
const pipeName = "blffqcl"
const fpAttempts = 4
var extractArgs = []string{fmt.Sprintf("-p=%s", pipeName), fmt.Sprintf("-r=%d", fpAttempts)}
//var extractArgs = []string{fmt.Sprintf("-p=%s", pipeName), "-t", "-f=testfp.json", fmt.Sprintf("-r=%d", fpAttempts)}
type fpChanResult struct {
fp []byte
err error
}
type bnlFingerprint struct {
FP *string `json:"fp"`
}
func getBNLFingerprint() (string, error) {
fpChan := make(chan fpChanResult)
go readFromPipe(fpChan)
if err := extractFp(); err != nil {
return "", err
}
result := <-fpChan
rfp, rerr := result.fp, result.err
if rerr != nil {
return "", rerr
}
bnl := &bnlFingerprint{}
if err := json.Unmarshal(rfp, bnl); err != nil {
return "", err
}
if bnl.FP == nil {
err := "Received null FP value indicating that FP retrieval process could not find FP"
logger.Errorf("%s: %s", GetCaller(), err)
return "", fmt.Errorf("%s", err)
}
tmpFp = *bnl.FP // set until it can be written to storage
return *bnl.FP, nil
}
func readFromPipe(c chan fpChanResult) {
lp, err := winio.ListenPipe(pipePrefix+pipeName, &winio.PipeConfig{
MessageMode: true,
InputBufferSize: 64,
OutputBufferSize: 64,
})
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error creating named pipe", GetCaller()), "error", err)
c <- fpChanResult{fp: []byte{}, err: err}
return
}
defer lp.Close()
pipe, err := lp.Accept()
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error accepting named pipe connection", GetCaller()), "error", err)
c <- fpChanResult{fp: []byte{}, err: err}
return
}
defer pipe.Close()
result, err := ioutil.ReadAll(bufio.NewReader(pipe))
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error reading from named pipe", GetCaller()), "error", err)
c <- fpChanResult{fp: []byte{}, err: err}
return
}
logger.Debugf("readFromPipe: Read %d bytes. FP (raw): %+x output: %s", len(result), result, string(result))
logger.Debugw("readFromPipe", "result", string(result))
c <- fpChanResult{fp: result, err: nil}
}
func extractFp() error {
// for executable source code see https://github.com/syncore/blff or qclauncher\resources\bin_src\
a, err := resources.Asset("../../resources/bin/blff/blff.exe") // "../../resources/bin/blff/blffconsole.exe"
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error reading FP extraction tool asset", GetCaller()), "error", err)
return err
}
outName := filepath.Join(getExecutingPath(), "ExtractBNLauncherFP.exe")
if err = ioutil.WriteFile(outName, a, 0644); err != nil {
logger.Errorw(fmt.Sprintf("%s: error extracting FP extraction tool", GetCaller()), "error", err)
return err
}
blff := exec.Command(outName, extractArgs...)
logger.Debugf("extractFp: Executing blff (%s) and awaiting completion...", outName)
if err = blff.Run(); err != nil {
logger.Errorw(fmt.Sprintf("%s: error occurred while running FP extraction tool", GetCaller()), "error", err)
return err
}
if err = os.Remove(outName); err != nil {
logger.Errorw(fmt.Sprintf("%s: error occurred while cleaning up FP extraction tool", GetCaller()), "error", err)
return err
}
if err = os.Remove(filepath.Join(getExecutingPath(), "blff_error.log")); err != nil && !os.IsNotExist(err) {
logger.Errorw(fmt.Sprintf("%s: error occurred while cleaning up FP extraction tool error log", GetCaller()), "error", err)
}
return nil
}
func isFPOverride() bool {
return ConfXSrcFp != XSrcFpDef
}