Skip to content

Commit 857189c

Browse files
authored
Merge pull request #9 from nccapo/refactor-package-naming
Refactor package naming
2 parents 03e21c6 + 2692b8c commit 857189c

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ nmap -vvv -sT -p 1-65535 {target_IP}
3939

4040
## Example Simple scanner
4141
<div align="center">
42-
<img src="/img/scanme.png" width="800px"</img>
42+
<img src="/assets/scanme.png" width="800px"</img>
4343
</div>
4444

4545
## Installation
4646

47-
- On Linux, install `libpcap`
47+
- On Linux, install `libpcap`
4848
```bash
4949
sudo apt install -y libpcap-dev
5050
```
@@ -122,7 +122,7 @@ func main() {
122122
## Sample scan
123123
```
124124
alessandro@xps:~/Development/scanme$ sudo go run main.go -ip 172.16.168.131
125-
[sudo] password for alessandro:
125+
[sudo] password for alessandro:
126126
2024/01/11 15:04:53 scanning ip 172.16.168.131 with interface vmnet8, gateway <nil>, src 172.16.168.1
127127
2024/01/11 15:04:53 ICMP Echo Reply received from 172.16.168.131
128128
2024/01/11 15:04:54 last port scanned for 172.16.168.131 dst port 65535 assuming we've seen all we can
@@ -147,5 +147,3 @@ Scanme is developed by Alessandro Bresciani with some help from various projects
147147
## Acknowledgments
148148
Inspired by and wanting to improve this https://github.com/google/gopacket/blob/master/examples/synscan/main.go
149149
Technical details checked here https://nmap.org/book/synscan.html and obviously https://github.com/nmap/nmap
150-
151-
File renamed without changes.
File renamed without changes.

utils/util.go renamed to internal/service/service.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1-
package utils
1+
// Package service is helper package for services which is external data and need to be parsed
2+
// to provide usefull information such as (port number, protocol and service name)
3+
package service
24

35
import (
46
"encoding/csv"
57
"fmt"
6-
"net"
78
"os"
89
)
910

10-
// Listen on port 0 to get a free port assigned by the system.
11-
// Get the actual address, including the assigned port.
12-
func GetFreeTCPPort() (int, error) {
13-
14-
listener, err := net.Listen("tcp", ":0")
15-
if err != nil {
16-
return 0, err
17-
}
18-
defer listener.Close()
19-
20-
addr := listener.Addr().(*net.TCPAddr)
21-
return addr.Port, nil
22-
}
23-
2411
// ServiceInfo represents information about a service
2512
type ServiceInfo struct {
2613
ServiceName string
@@ -32,7 +19,7 @@ type ServiceInfo struct {
3219
// https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv
3320
// and returns the service name for a given port number and protocol
3421
func GetServiceName(port, proto string) (string, error) {
35-
file, err := os.Open("data/services.csv")
22+
file, err := os.Open("api/services.csv")
3623
if err != nil {
3724
return "", err
3825
}

internal/tcp/tcp.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Package tcp provided way to get actual address including the assigned port
2+
package tcp
3+
4+
import "net"
5+
6+
// Listen on port 0 to get a free port assigned by the system.
7+
// Get the actual address, including the assigned port.
8+
func GetFreeTCPPort() (int, error) {
9+
listener, err := net.Listen("tcp", ":0")
10+
if err != nil {
11+
return 0, err
12+
}
13+
defer listener.Close()
14+
15+
addr := listener.Addr().(*net.TCPAddr)
16+
return addr.Port, nil
17+
}

scanme/scanner.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"sync"
99
"time"
1010

11-
"github.com/CyberRoute/scanme/utils"
11+
"github.com/CyberRoute/scanme/internal/service"
12+
"github.com/CyberRoute/scanme/internal/tcp"
1213
"github.com/google/gopacket"
1314
"github.com/google/gopacket/layers"
1415
"github.com/google/gopacket/pcap"
@@ -169,7 +170,7 @@ func (s *Scanner) sendARPRequest() (net.HardwareAddr, error) {
169170

170171
func getFreeTCPPort() (layers.TCPPort, error) {
171172
// Use the library or function that returns a free TCP port as an int.
172-
tcpport, err := utils.GetFreeTCPPort()
173+
tcpport, err := tcp.GetFreeTCPPort()
173174
if err != nil {
174175
return 0, err
175176
}
@@ -396,7 +397,7 @@ func (s *Scanner) ConnScan() (map[layers.TCPPort]string, error) {
396397
conn, err := net.DialTimeout("tcp", addr, 3*time.Second)
397398
if err == nil {
398399
conn.Close()
399-
serviceName, err := utils.GetServiceName(strconv.Itoa(p), "tcp")
400+
serviceName, err := service.GetServiceName(strconv.Itoa(p), "tcp")
400401
if err != nil {
401402
// Log or handle the error, and continue the loop
402403
log.Printf("Error getting service name for port %d: %v", p, err)

utils/doc.go

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)