archwhite
8/8/2019 - 8:26 AM

Read text file with encoding autodetection

Replace substring in text file using regexp pattern

import sys
import re
import locale
import chardet # pip install chardet

print('locale pref encoding = ', locale.getpreferredencoding())
print('sys filesyste encoding = ', sys.getfilesystemencoding())

PROTOCOL = "https"
IP_PORT = "localhost"
TARGET = "target.js"

def print_help():
	print('Error: Bad parameters')
	print('Use: <program> <target> <ip>[:port] [http/https]')
	print('e.g.: main.py main.chunk.js 172.16.0.1 https')
	print('e.g.: main.py main.chunk.js 172.16.0.1:8080 https')
	exit(1)

if len(sys.argv) == 3: # only ip address
	TARGET = sys.argv[1]
	IP_PORT = sys.argv[2]

elif len(sys.argv) == 4: # ip address and http protocol
	TARGET = sys.argv[1]
	IP_PORT = sys.argv[2]
	PROTOCOL = sys.argv[3]

else: # wrong amount of params
	print_help()

# Read in the file
filename = TARGET
pattern = r'http.{0,1}://.*?/api'

with open(filename, 'rb') as file:
	filedata = file.read()

enc = chardet.detect(filedata)['encoding']
filedata = filedata.decode(enc) # decode bytes to string with enc

# Replace the target string
filedata = re.sub(pattern, f"{PROTOCOL}://{IP_PORT}/api", filedata)

# Write the file out again
with open(filename, 'w') as file:
	file.write(filedata)
PROTOCOL = "https"
IP_PORT = "localhost"
TARGET = "target.js"

def print_help()
	puts 'Error: Bad parameters'
	puts 'Use: <program> <target> <ip>[:port] [http/https]'
	puts 'e.g.: main.py main.chunk.js 172.16.0.1 https'
	puts 'e.g.: main.py main.chunk.js 172.16.0.1:8080 https'
	exit 1
end

filename = 'main.chunk.js'
file = File.open(filename, "rb")
file = file.read

p file.encoding.name
file = file.encode('utf-8', 'utf-16')

value = file.sub(/http/, "HTTP")

File.write('output.js', value)
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace IPchanger
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string filename = args[0];
                string strIpPort = args[1];

                string protocol = "https";
                if (args.Length > 2)
                    protocol = args[2];

                string data = File.ReadAllText(filename);
                string pattern = "http.{0,1}://.*?/api";
                string newData = Regex.Replace(data, pattern, string.Concat(protocol, "://", strIpPort, "/api"));
                File.WriteAllText(filename, newData);
            } catch (Exception ex) {
                Console.WriteLine(ex);
            }
        }
    }
}
package main

import (
    "fmt"
	"io/ioutil"
	"regexp"
	"unicode/utf8"
	"golang.org/x/text/transform"
	"golang.org/x/text/encoding/unicode"
)

func log(line string) {
	fmt.Println(line)
}

func main() {
    rawdata, err := ioutil.ReadFile("main.chunk.js") // just pass the file name
    if err != nil {
        fmt.Print(err)
    }

	// bs_UTF16LE, _, _ := transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder(), rawdata))
	// bs_UTF16BE, _, _ := transform.Bytes(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder(), rawdata))
	//bs_UTF8LE, _, _ := transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder(), bs_UTF16LE)
	// bs_UTF8BE, _, _ := transform.Bytes(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder(), bs_UTF16BE)
	
	utf8filedata, _, _ := transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder(), rawdata)
	strfile := string(utf8filedata)

	pattern := "http.{0,1}://.*?/api"
	fmt.Println(utf8.ValidString(strfile))   
	r := regexp.MustCompile(pattern)
	if r.MatchString(strfile) {
		log("true")
	} else {
		log("false")
	}
	fmt.Println(r.FindString(strfile)) 
}