|
package main |
|
|
|
import ( |
|
"bufio" |
|
"bytes" |
|
"os" |
|
"fmt" |
|
"io" |
|
"strings" |
|
"net/http" |
|
) |
|
|
|
func main() { |
|
url := "https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt" |
|
filepath := "/tmp/socks5.txt" |
|
proxychainsConfigPath := "/etc/proxychains4.conf" |
|
keyword := "[ProxyList]" |
|
|
|
// Download the file |
|
err := downloadFile(url, filepath) |
|
if err != nil { |
|
fmt.Printf("Failed to download file: %v\n", err) |
|
return |
|
} |
|
|
|
// Transform the content |
|
transformedLines, err := transformContent(filepath) |
|
if err != nil { |
|
fmt.Printf("Failed to transform content: %v\n", err) |
|
return |
|
} |
|
|
|
// Print the transformed lines |
|
for _, line := range transformedLines { |
|
fmt.Println(line) |
|
} |
|
|
|
// Open the file for reading and writing |
|
f, err := os.OpenFile(proxychainsConfigPath, os.O_RDWR, 0644) |
|
if err != nil { |
|
panic(err) |
|
} |
|
defer f.Close() |
|
|
|
// Create a buffer to hold the lines before the keyword |
|
var bs []byte |
|
buf := bytes.NewBuffer(bs) |
|
|
|
// Create a scanner to read the file line by line |
|
scanner := bufio.NewScanner(f) |
|
|
|
stopWriting := false |
|
|
|
// Read and process each line |
|
for scanner.Scan() { |
|
text := scanner.Text() |
|
if text == keyword { |
|
stopWriting = true // Stop writing to the buffer after finding the keyword |
|
buf.WriteString(text + "\n") // Write the keyword line to the buffer |
|
} else if !stopWriting { |
|
buf.WriteString(text + "\n") |
|
} else { |
|
for _, line := range transformedLines { |
|
buf.WriteString(line + "\n") |
|
} |
|
} |
|
} |
|
|
|
// Truncate the file and write the buffer content back to the file |
|
f.Truncate(0) |
|
f.Seek(0, 0) |
|
buf.WriteTo(f) |
|
} |
|
|
|
func downloadFile(url, filepath string) error { |
|
// Send a GET request to the URL |
|
resp, err := http.Get(url) |
|
if err != nil { |
|
return err |
|
} |
|
defer resp.Body.Close() |
|
|
|
// Create the file |
|
out, err := os.Create(filepath) |
|
if err != nil { |
|
return err |
|
} |
|
defer out.Close() |
|
|
|
// Write the body to file |
|
_, err = io.Copy(out, resp.Body) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
|
|
func transformContent(filepath string) ([]string, error) { |
|
// Open the file |
|
file, err := os.Open(filepath) |
|
if err != nil { |
|
return nil, err |
|
} |
|
defer file.Close() |
|
|
|
var transformedLines []string |
|
scanner := bufio.NewScanner(file) |
|
for scanner.Scan() { |
|
line := scanner.Text() |
|
parts := strings.Split(line, ":") |
|
if len(parts) != 2 { |
|
continue // Skip lines that do not match the "ip:port" format |
|
} |
|
transformed := fmt.Sprintf("socks5\t%s\t%s", parts[0], parts[1]) |
|
transformedLines = append(transformedLines, transformed) |
|
} |
|
|
|
return transformedLines, scanner.Err() |
|
} |