GolangNote

Golang笔记

golang 为Windows XP/Server 2003 编译程序

Permalink1

一般生产环境中很少用 32 位的 windows 系统,为了老用户,还是做一下跨平台编译。

golang 为Windows XP/Server 2003 编译程序

Bash: GOARCH=386
1
GOOS=windows GOARCH=386 go build -o yourapp_32bit.exe

Related articles

Golang 单实例实现网站多域名请求

有时候写网站,为了统一的后端,把不同业务都集中到一个后端,这时就需要处理多域名的请求,在 Go http server 里实现很简单,只需把不同域名映射到不同的 `http.Handler`。...

Golang 时区时差处理方式

个人习惯用 0 时区时间戳记录时间,可以方便转到不同时区,下面介绍 Golang 时区时差处理...

Comments

There are 1 Comments to "golang 为Windows XP/Server 2003 编译程序"

1 L says:
回复

Go: PortScan
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
package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"
)

// 接收的参数
var (
	help bool
	targets string
	portRanges string
	numOfgoroutine int
	file string
)


// Usage
func usage() {
	fmt.Fprintf(
		os.Stderr, `Author: Loong716
Github: https://github.com/loong716
Usage: PortScan [-h Help] [-t IP/CIDR] [-p Ports] [-n Threads] [-o Output]

Options:
`)
	flagSet := flag.CommandLine
	order := []string{"h", "t", "p", "n", "o"}
	for _, name := range order {
		fl4g := flagSet.Lookup(name)
		fmt.Printf("  -%s  ", fl4g.Name)
		fmt.Printf("  %s\n", fl4g.Usage)
	}
}


// 初始化参数
func init() {
	flag.BoolVar(&help, "h", false, "Print the help page")
	flag.StringVar(&file, "o", "", "Output file path")
	flag.StringVar(&targets, "t", "", "Targets, Single IP or CIDR")
	flag.StringVar(&portRanges, "p", "", "Port ranges. Ex: 1-65535 or 80,443 or 80,443,100-110")
	flag.IntVar(&numOfgoroutine, "n", 256, "The Number of Goroutine, too large value is not recommended (Default is 256)")

	// 替换默认的Usage
	flag.Usage = usage
}


func inc(ip net.IP) {
	for j := len(ip) - 1; j >= 0; j-- {
		ip[j]++
		if ip[j] > 0 {
			break
		}
	}
}


// 将CIDR转换为IP,若用户输入为单个IP直接返回
func CIDR2IP(cidr string) ([]string, error) {
	var hosts []string
	if !strings.ContainsAny(cidr, "/") {
		hosts = append(hosts, cidr)
		return hosts, nil
	}

	ip, ipnet, err := net.ParseCIDR(cidr)
	if err != nil {
		fmt.Println("Error: Invalid CIDR! Please use '-h' to see usage.")
		return nil, err
	}

	for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
		hosts = append(hosts, ip.String())
	}
	return hosts[1 : len(hosts)-1], nil
}


// 解析用户输入的端口范围
func ParsePortRange(portList string) ([]int, error) {
	var ports []int
	// 先以','分割
	portList2 := strings.Split(portList, ",")

	for _, i := range portList2 {
		// 如果存在'-',则解析范围
		if strings.ContainsAny(i, "-") {
			a := strings.Split(i, "-")
			startPort, err := strconv.Atoi(a[0])
			if err != nil {
				fmt.Println("Error: StartPort strconv error! Please use '-h' to see usage.")
				os.Exit(1)
			}
			endPort, err := strconv.Atoi(a[1])
			if err != nil {
				fmt.Println("Error: EndPort strconv error! Please use '-h' to see usage.")
				os.Exit(1)
			}
			for j := startPort; j <= endPort; j++ {
				ports = append(ports, j)
			}
		} else {
			// 不存在'-',直接加入ports
			singlePort, err := strconv.Atoi(i)
			if err != nil {
				fmt.Println("Error: SinglePort strconv error! Please use '-h' to see usage.")
				os.Exit(1)
			}
			ports = append(ports, singlePort)
		}
	}
	return ports, nil
}


// 探测端口是否开放
func isOpen(target string) bool {
	conn, err := net.DialTimeout("tcp", target, time.Millisecond*time.Duration(200))
	if err != nil {
		return false
	}
	_ = conn.Close()
	return true
}


func portScan(hosts []string, ports []int) int {
	wg := sync.WaitGroup{}
	targetsChan := make(chan string, 100)
	poolCount := numOfgoroutine  // goroutine的数量

	// 创建goroutine
	for i := 0; i <= poolCount; i++ {
		go func() {
			for j := range targetsChan {
				openFlag := isOpen(j)
				if openFlag {
                    fmt.Printf("%s\n", j)
				}
				wg.Done()
			}
		}()
	}

	// 将ip与port拼接为target,通过channel传输给goroutine
	for _, m := range ports {
		portString := strconv.Itoa(m)
		for _, n := range hosts {
			target := n + ":" + portString
			targetsChan <- target
			wg.Add(1)
		}
	}

	wg.Wait()
	return 0
}


func main() {
	startTime := time.Now()
	flag.Parse()

	// 没有参数输入或输入'-h'时输出Usage
	if flag.NFlag() == 0 || help {
		flag.Usage()
		os.Exit(1)
	}

	hosts, err := CIDR2IP(targets)
	if err != nil {
		fmt.Println("Error: CIDR conversion failed! Please use '-h' to see usage.")
		os.Exit(1)
	}


	ports, err := ParsePortRange(portRanges)
	if err != nil {
		fmt.Println("Error: ParsePortRange failed! Please use '-h' to see usage.")
		os.Exit(1)
	}

	// 如果设置了'-o'参数,则使用log模块输出到文件
	if file != "" {
		logFile, err := os.OpenFile(file, os.O_RDWR | os.O_CREATE | os.O_APPEND, os.ModeAppend | os.ModePerm)
		if err != nil {
			fmt.Println("Error: Open output file failed!")
			os.Exit(1)
		}

		defer logFile.Close()
		out := io.MultiWriter(os.Stdout, logFile)
		log.SetOutput(out)
		fmt.Printf("# Port Scan Start...\n\n")
		portScan(hosts, ports)
		spendTime := time.Since(startTime).Seconds()
		fmt.Printf("\n# Finished. Take %.4fs.\n", spendTime)
	} else {
		fmt.Printf("# Port Scan Start...\n\n")
		portScan(hosts, ports)
		spendTime := time.Since(startTime).Seconds()
		fmt.Printf("\n# Finished. Take %.4fs.\n", spendTime)
	}

}

Write a Comment to "golang 为Windows XP/Server 2003 编译程序"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.3 Processed in 2ms