This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
/
smb.go
91 lines (77 loc) · 1.62 KB
/
smb.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
package connector
import (
"io"
"net"
"os"
"time"
"github.com/hirochachacha/go-smb2"
)
//OpenSMBConnection OpenSMBConnection
func OpenSMBConnection(ipAddress string, username string, password string) (*smb2.Session, error) {
dialer := net.Dialer{Timeout: time.Second * 5}
conn, err := dialer.Dial("tcp", ipAddress+":445")
if err != nil {
return nil, err
}
d := &smb2.Dialer{
Initiator: &smb2.NTLMInitiator{
User: username,
Password: password,
},
}
s, err := d.Dial(conn)
if err != nil {
return nil, err
}
return s, nil
}
//PutFileSMB PutFileSMB
func PutFileSMB(session *smb2.Session, localPath string, remotePath string, remoteDisk string) bool {
fs, err := session.Mount(remoteDisk + "$")
if err != nil {
return false
}
defer fs.Umount()
f, err := fs.Create(remotePath)
if err != nil {
return false
}
defer f.Close()
srcFile, err := os.Open(localPath)
if err != nil {
return false
}
defer srcFile.Close()
_, err = io.Copy(f, srcFile)
if err != nil {
return false
}
return true
}
//GetFileSMB GetFileSMB
func GetFileSMB(session *smb2.Session, localPath string, remotePath string, remoteDisk string) bool {
fs, err := session.Mount(remoteDisk + "$")
if err != nil {
return false
}
defer fs.Umount()
f, err := fs.Open(remotePath)
if err != nil {
return false
}
defer f.Close()
_, err = os.Stat(localPath)
if os.IsNotExist(err) {
os.Create(localPath)
}
srcFile, err := os.OpenFile(localPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return false
}
defer srcFile.Close()
_, err = io.Copy(srcFile, f)
if err != nil {
return false
}
return true
}