2018-05-22 06:20:07 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 jun7@hush.mail
|
|
|
|
|
|
|
|
This file is part of wyebadblock.
|
|
|
|
|
|
|
|
wyebadblock is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
wyebadblock is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with wyebadblock. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-22 06:01:28 +00:00
|
|
|
#include "wyebrun.h"
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
# define D(f, ...) g_print("#"#f"\n", __VA_ARGS__);
|
|
|
|
# define DD(a) g_print("#"#a"\n");
|
|
|
|
#else
|
|
|
|
# define D(f, ...) ;
|
|
|
|
# define DD(a) ;
|
|
|
|
#endif
|
|
|
|
|
2018-05-22 06:43:05 +00:00
|
|
|
|
2018-05-22 06:01:28 +00:00
|
|
|
#if ISEXT
|
|
|
|
|
|
|
|
#include <webkit2/webkit-web-extension.h>
|
|
|
|
static bool check(const char *requri, const char *pageuri)
|
|
|
|
{
|
|
|
|
char *uris = g_strconcat(requri, " ", pageuri, NULL);
|
2018-06-06 05:10:35 +00:00
|
|
|
char *ruri = wyebget(EXENAME, uris);
|
2018-05-22 06:01:28 +00:00
|
|
|
g_free(uris);
|
|
|
|
|
|
|
|
if (ruri && !*ruri) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-22 06:20:07 +00:00
|
|
|
static gboolean reqcb(WebKitWebPage *kp, WebKitURIRequest *req,
|
2018-05-22 06:01:28 +00:00
|
|
|
WebKitURIResponse *r, gpointer p)
|
|
|
|
{
|
2018-05-22 06:20:07 +00:00
|
|
|
if (g_object_get_data(G_OBJECT(kp), "adblock") == (gpointer)'n')
|
2018-05-22 06:01:28 +00:00
|
|
|
return false;
|
|
|
|
|
2018-06-05 17:58:02 +00:00
|
|
|
static bool first = true;
|
2018-05-22 06:01:28 +00:00
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
if (webkit_uri_request_get_http_headers(req))
|
|
|
|
first = false;
|
|
|
|
else //no head is local data. so haven't to block
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-22 06:20:07 +00:00
|
|
|
if (check(webkit_uri_request_get_uri(req),
|
|
|
|
webkit_web_page_get_uri(kp))) return false;
|
2018-05-22 06:01:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
static gboolean keepcb(WebKitWebPage *kp)
|
2018-05-22 06:01:28 +00:00
|
|
|
{
|
|
|
|
if (g_object_get_data(G_OBJECT(kp), "adblock") != (gpointer)'n')
|
2018-06-06 05:10:35 +00:00
|
|
|
wyebkeep(EXENAME, 30);
|
2018-05-22 06:01:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool apimode = false;
|
|
|
|
static void pageinit(WebKitWebExtension *ex, WebKitWebPage *kp)
|
|
|
|
{
|
|
|
|
DD(pageinit)
|
|
|
|
|
|
|
|
if (!apimode)
|
|
|
|
g_signal_connect(kp, "send-request", G_CALLBACK(reqcb), NULL);
|
|
|
|
|
|
|
|
g_object_set_data(G_OBJECT(kp), "wyebcheck", check);
|
|
|
|
|
2018-06-05 09:34:05 +00:00
|
|
|
keepcb(kp);
|
2018-05-25 04:39:58 +00:00
|
|
|
g_object_weak_ref(G_OBJECT(kp), (GWeakNotify)g_source_remove,
|
2018-06-05 09:34:05 +00:00
|
|
|
GUINT_TO_POINTER(g_timeout_add(11 * 1000, (GSourceFunc)keepcb, kp)));
|
2018-05-22 06:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT void webkit_web_extension_initialize_with_user_data(
|
|
|
|
WebKitWebExtension *ex, const GVariant *v)
|
|
|
|
{
|
|
|
|
bool hasarg = false;
|
|
|
|
const char *str;
|
|
|
|
if (v && g_variant_is_of_type((GVariant *)v, G_VARIANT_TYPE_STRING) &&
|
|
|
|
(str = g_variant_get_string((GVariant *)v, NULL)))
|
|
|
|
{
|
|
|
|
bool enable = true;
|
|
|
|
char **args = g_strsplit(str, ";", -1);
|
|
|
|
for (char **arg = args; *arg; arg++)
|
|
|
|
{
|
|
|
|
if (g_str_has_prefix(*arg, "adblock:"))
|
|
|
|
{
|
|
|
|
enable = !strcmp(*arg + 8, "true");
|
|
|
|
hasarg = true;
|
|
|
|
}
|
|
|
|
if (!strcmp(*arg, "wyebabapi"))
|
|
|
|
apimode = true;
|
|
|
|
}
|
|
|
|
g_strfreev(args);
|
|
|
|
if (!enable) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasarg && *(g_getenv("DISABLE_ADBLOCK") ?: "") != '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_signal_connect(ex, "page-created", G_CALLBACK(pageinit), NULL);
|
|
|
|
}
|
|
|
|
|
2018-05-22 06:43:05 +00:00
|
|
|
|
|
|
|
|
2018-05-22 06:01:28 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include "ephy-uri-tester.c"
|
|
|
|
|
|
|
|
static EphyUriTester *tester = NULL;
|
|
|
|
static GThread *initt = NULL;
|
|
|
|
|
2018-06-06 07:40:52 +00:00
|
|
|
static gpointer inittcb(gpointer p)
|
2018-05-22 06:01:28 +00:00
|
|
|
{
|
|
|
|
ephy_uri_tester_load(tester);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void monitorcb(
|
|
|
|
GFileMonitor *m, GFile *f, GFile *o, GFileMonitorEvent e, gpointer p)
|
|
|
|
{
|
|
|
|
if (e == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT ||
|
|
|
|
e == G_FILE_MONITOR_EVENT_DELETED)
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-06-06 07:40:52 +00:00
|
|
|
static bool tryload(const char *parent)
|
2018-05-22 06:01:28 +00:00
|
|
|
{
|
2018-06-06 07:40:52 +00:00
|
|
|
char *path = g_build_filename(parent, DIRNAME, LISTNAME, NULL);
|
2018-05-22 06:01:28 +00:00
|
|
|
|
|
|
|
GFile *gf = g_file_new_for_path(path);
|
|
|
|
GFileMonitor *gm = g_file_monitor_file(gf,
|
|
|
|
G_FILE_MONITOR_NONE, NULL, NULL);
|
|
|
|
g_signal_connect(gm, "changed", G_CALLBACK(monitorcb), NULL);
|
|
|
|
g_object_unref(gf);
|
|
|
|
|
|
|
|
if (g_file_test(path, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
filter_file = g_file_new_for_path(path);
|
|
|
|
tester = ephy_uri_tester_new("/foo/bar");
|
|
|
|
initt = g_thread_new("init", inittcb, NULL);
|
|
|
|
}
|
|
|
|
g_free(path);
|
2018-06-06 07:40:52 +00:00
|
|
|
return tester;
|
|
|
|
}
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
DD(wyebad init)
|
|
|
|
if (tryload(g_get_user_config_dir())) return;
|
|
|
|
|
|
|
|
for (const gchar * const *d = g_get_system_data_dirs(); *d; d++)
|
|
|
|
if (tryload(*d)) return;
|
|
|
|
|
|
|
|
char *dir = g_build_filename(g_get_user_config_dir(), DIRNAME, NULL);
|
|
|
|
if (!g_file_test(dir, G_FILE_TEST_EXISTS))
|
|
|
|
g_mkdir_with_parents(dir, 0700);
|
|
|
|
g_free(dir);
|
2018-05-22 06:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *datafunc(char *req)
|
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
static GMutex datam;
|
2018-06-04 20:54:27 +00:00
|
|
|
g_mutex_lock(&datam);
|
|
|
|
|
2018-05-22 06:01:28 +00:00
|
|
|
if (initt)
|
|
|
|
{
|
|
|
|
g_thread_join(initt);
|
|
|
|
initt = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//req uri + ' ' + page uri
|
|
|
|
char **args = g_strsplit(req, " ", 2);
|
|
|
|
|
|
|
|
char *ret = !tester ? g_strdup(args[0]) :
|
|
|
|
ephy_uri_tester_rewrite_uri(tester, args[0], args[1] ?: args[0]);
|
|
|
|
|
|
|
|
g_strfreev(args);
|
|
|
|
|
2018-05-23 15:13:15 +00:00
|
|
|
#if DEBUG
|
|
|
|
if (ret)
|
|
|
|
D(ret %s, ret)
|
|
|
|
else
|
|
|
|
D(BLOCKED %s, req)
|
|
|
|
#endif
|
|
|
|
|
2018-06-04 20:54:27 +00:00
|
|
|
g_mutex_unlock(&datam);
|
2018-05-22 06:01:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
DD(This bin is compiled with DEBUG=1)
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
wyebclient(argv[0]);
|
|
|
|
}
|
|
|
|
else if (g_str_has_prefix(argv[1], WYEBPREFIX))
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
wyebsvr(argc, argv, datafunc);
|
|
|
|
}
|
2018-06-07 05:46:26 +00:00
|
|
|
else if (!strcmp(argv[1], "-css") || !strcmp(argv[1], "--css"))
|
2018-05-22 06:01:28 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
g_thread_join(initt);
|
|
|
|
|
2018-06-06 04:29:14 +00:00
|
|
|
g_print("%s", tester->blockcss->str);
|
2018-06-07 05:42:26 +00:00
|
|
|
g_print("\n\n\n\n{display:none !important}\n"
|
2018-06-07 10:15:49 +00:00
|
|
|
"\n/*\n"
|
|
|
|
"{height:0 !important; visibility:hidden !important;}\n"
|
|
|
|
"\n*/\n");
|
2018-05-22 06:01:28 +00:00
|
|
|
//g_print(tester->blockcssprivate->str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-05 09:34:05 +00:00
|
|
|
wyebkeep(argv[0], 30);
|
|
|
|
g_print("%s", wyebget(argv[0], argv[1]));
|
2018-05-22 06:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|