Squid's URL modification/rewrite - url

I need to modify some special URLs which are passed over Squid, for example: i access the address www.google.com.vn through out my Squid. I want to modify Squid source code at some where to replace www.google.com.vn into www.google.com. So every requests to www.google.com.vn will become requests to www.google.com
Please help ASPS

First of all, squid rewriting and redirecting depends on a third party helper.
The best part is that you can write this helper in any programming language.
use the url_rewrite_program directive in squid.conf
and add to it the path to your custom made script.
Your script will receive content with the following:
URL client_ip "/" fqdn user method [ kvpairs]\n
Obviously you need the URL part,so figure a way to get the url part and return the website you want the client to be directed to.
Hope that help in any way...
Example rewrite programm (C++)
#include <iostream>
#include <string>
using namespace std;
// a replace function :)
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
int main()
{
while(1)
{
string input;
cin >> input;
replace(input, "www.google.vn", "www.google.com");
cout << input << endl;
}
}

Related

flex 2.5.35 gives error when ctrl-M used in lex file

I have a simple lex file.
%{
#include <stdio.h>
%}
space_char [ \t\^M]
space {space_char}+
%%
%%
int yywrap(void) {
return 1;
}
int main(void) {
yylex();
return 0;
}
When I compile this file with flex-2.5.35, it gives following errors:
lex.l:5: bad character:
lex.l:5: name defined twice
But, with flex-2.5.4, it runs fine.
I understand this error is due to special character ctrl-m (carriage-return). I want to know if flex-2.5.35 doesn't support special characters like ctrl-l, ctrl-m? And if so, then what's the alternate way? Please note, I am restricted with the use of 2.5.35 only.
Thanks.
As in C, you can use \r for the carriage return character.

Services and Control points using libupnp SDK for ubuntu 12.04

How to make a service or control point using libupnp SDK?!
I didn't understand the documentation attached with this SDK, I don't know from where to start ?!
Can you help me in that and refer me to some helping tutorials ?!
Thanks!
Yes I know, this is a very old question but still had no answer. I came here from google because I also was looking for an entry to use the libupnp SDK aka Portable SDK for UPnP* Devices, but without success. So I will share how I got it with some effort to find things.
I'm using Debian Stretch. So first I installed:
~$ sudo apt install libupnp6 libupnp6-dev libupnp6-doc
Then have a look at the documentation (of course ;) in /usr/share/doc/libupnp6-doc/html/modules.html. One of the problems was that I wasn't able to find this document online. With the UPnP API I started with this simple program, that searches for upnp devices on the local network. Just set INTERFACE[] and SEARCH_DURATION_SECONDS for what you need.
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <upnp/upnp.h>
constexpr char INTERFACE[] = "eth0";
constexpr int SEARCH_DURATION_SECONDS = 5;
int callback_event_handler(Upnp_EventType event_type, void* event, void* cookie)
{
std::string str;
switch (event_type) {
case UPNP_DISCOVERY_SEARCH_RESULT:
str = "UPNP_DISCOVERY_SEARCH_RESULT";
break;
case UPNP_DISCOVERY_SEARCH_TIMEOUT:
str = "UPNP_DISCOVERY_SEARCH_TIMEOUT";
break;
default:
std::cout << "Called callback_event_handler with ignored event" << std::endl;
return 0;
}
std::cout << "Executing callback_event_handler with event type " << str << std::endl;
std::cout << (char*)cookie << std::endl;
return 0;
}
int main()
{
constexpr char cookie[] = "Free custom data, usable in the callback function.";
UpnpClient_Handle client_handle = -1;
UpnpInit2(INTERFACE, 0);
UpnpRegisterClient(callback_event_handler, &cookie, &client_handle);
UpnpSearchAsync(client_handle, SEARCH_DURATION_SECONDS, "upnp:rootdevice", cookie);
std::this_thread::sleep_for(std::chrono::seconds(SEARCH_DURATION_SECONDS + 1));
UpnpFinish();
}
The search target (ST) upnp:rootdevice at UpnpSearchAsync(..) determins what to search. For other search targets look at UPnP Device Architecture 1.0, chapter 1.2.2 Discovery: Search: Request with M-SEARCH.
I compiled the program with:
~$ g++ -std=c++11 -pedantic-errors -Wall -lpthread -lupnp upnpsearch.cpp -o upnpsearch
If you execute it and you have UPnP devices on your local network you should get an output something like this:
Executing callback_event_handler with event type UPNP_DISCOVERY_SEARCH_RESULT
Free custom data, usable in the callback function.
Called callback_event_handler with ignored event
Called callback_event_handler with ignored event
Executing callback_event_handler with event type UPNP_DISCOVERY_SEARCH_RESULT
Free custom data, usable in the callback function.
Executing callback_event_handler with event type UPNP_DISCOVERY_SEARCH_TIMEOUT
Free custom data, usable in the callback function.
Here you see that two UPnP devices are online.

LuaBind and package.loadlib

I'm trying to go through the tutorial with luabind here, http://www.rasterbar.com/products/luabind/docs.html, however i'm having trouble loading the library. I'm currently using version 5.1 of lua, so I believe I would use package.loadlib instead of loadlib. I made a simple dll which is this:
#include <iostream>
#include <luabind\luabind.hpp>
void greet()
{
std::cout << "Hello world!\n";
}
extern "C" int init(lua_State* L)
{
luabind::open(L);
luabind::module(L)
[
luabind::def("greet", &greet)
];
return 0;
}
This builds just fine. However I get an error in lua when I try to run this code:
package.loadlib("LuaTestLib.dll", "init")
greet()
It states that greet is nil. How do I load the functions from the dll properly?
From the first two sentences of package.loadlib's documentation:
Dynamically links the host program with the C library libname. Inside this library, looks for a function funcname and returns this function as a C function.
(emphasis added)
This doesn't execute funcname. It simply returns it as a function for you to call. You still have to call it:
package.loadlib("LuaTestLib.dll", "init")()

Complete URL encoding

Anyone know of a tool to completely encode a string to URL encoding? Best known example is something to convert space character to %20. I want to do this for every single character. What's a good tool for this (linux)?
thanks everyone for down voting, if i cared what language i would have specified. couldnt find anything useful in the other post linked below so i wrote this. this is good enough for me, might be good enough for you.
#include <stdio.h>
// Treats all args as one big string. Inserts implicit spaces between args.
int main(int argc, char *argv[])
{
if(argc == 1)
{
printf("Need something to encode.");
return 1;
}
int count = 0;
while(++count < argc)
{
char *input = argv[count];
while(*input != '\0')
{
printf("%%%x", *input);
input++;
}
printf("%%20");
}
printf("\n");
return 0;
}
Take a look at this SO question:
How to urlencode data for curl command?
Which programming language? You can even do something client-side...
i modified this of the other link
perl -p -e 's/(.)/sprintf("%%%02X", ord($1))/seg'
it works nice enough..
run this.. type in what you want to convert..(or pipe it through) and it'll output everything %encoded

Swig and Lua: how to map Lua file to FILE*

I have a C function that takes FILE* as an argument and I'd like to use this function in Lua, passing Lua file. I guess I need a %typemap for this. How to write it?
(I just started learning Lua).
Here is the solution that I finally came up with.
In Lua source, in liolib.c, there is a function FILE *tofile (lua_State *L), which converts Lua file to C FILE*, but it's not a part of the API. I modified it a bit to make a typemap:
%typemap(in) FILE * {
FILE **f;
if (lua_isnil(L, $input))
$1=NULL;
else {
f = (FILE **)luaL_checkudata(L, $input, "FILE*");
if (*f == NULL)
luaL_error(L, "attempt to use a closed file");
$1=*f;
}
}
This typemap accepts also nil, because I needed a way to pass NULL to the C function.
You're using SWIG to generate Lua bindings for your C code? Why not use the Lua C API directly, or if you can use C++, Luabind? I think either of those would be better than trying to make it work with SWIG, unless you've got a strong attachment to SWIG already.
There's no easy way to do what you're asking.
The Lua File class interface abstracts the underlying implementation. You cannot simply typemap it. You can, however, create a C proxy that wraps FILE operations you need and create an instance of this proxy in Lua using SWIG. You can then generate a typemap to convert a FILE* to a wrapper proxy instance.
Something like:
class MyFileProxy {
private:
FILE* fp;
public:
MyFileProxy(FILE* fp);
MyFileProxy(const char* path);
FILE* GetFilePointer();
Seek(...
In SWIG the binding is simply:
%module "MyFile"
%{
#include "MyFileProxy.h"
%}
// Tell SWIG how to use a proxy for functions that take a FILE*
%typemap(in) FILE*
{
void* tmp = 0;
SWIG_ConvertPtr(L,$argnum,(void**)&tmp,$1_descriptor,1);
if (tmp)
{
MyFileProxy* proxy = (MyFileProxy)tmp;
arg$argnum = proxy->GetFilePointer();
}
}
// Tell SWIG how to create a proxy when returning FILE*
%typemap(out) FILE*
{
MyFileProxy* pResult = new MyFileProxy($arg);
SWIG_NewPointerObj(L, pResult, $1_descriptor, 1);
}
%include "MyFileProxy.h
}
You won't be able to use io:File directly, however.

Resources