dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.7k stars 220 forks source link

Support curl-impersonate #949

Closed dzasa closed 1 month ago

dzasa commented 1 month ago

Describe you feature request

Is there a plan to support curl-impersonate https://github.com/lwthiker/curl-impersonate/tree/main ?

I managed to run manually php commands with curl impersonate, but not with frankenphp

dunglas commented 1 month ago

I don't see how it's related to FrankenPHP. If it works with a standard PHP build, it should work with FrankenPHP as well. If it's not the case, could you provide a reproducer (if possible a minimal Docker image) and the related logs?

Thanks.

dzasa commented 1 month ago

Thanks @dunglas for fast response :). It might not be improvement, I thought so it was, but more like "I dont know how to implement it with frankenphp".

This is instruction how to make it work with PHP https://github.com/lwthiker/curl-impersonate/blob/main/docs/03_LIBCURL_IMPERSONATE_PHP.md

And as I take frankenphp in dockerfile FROM dunglas/frankenphp:1-php8.3 AS php_upstream

I managed to make it work with php with

RUN git clone https://github.com/lwthiker/curl-impersonate.git
WORKDIR curl-impersonate
RUN mkdir build
WORKDIR build
RUN ../configure
RUN make chrome-build
RUN make chrome-install
RUN ldconfig
WORKDIR /app/curl-impersonate
RUN rm -Rf build

RUN patchelf --set-soname libcurl.so /usr/local/lib/libcurl-impersonate-chrome.so
RUN echo "/usr/local/lib/libcurl-impersonate-chrome.so" > /etc/ld.so.preload

And when I run

php -r 'print_r(curl_version());'

It gives me

[ssl_version] => BoringSSL

But when I make http request which is handled by frankenphp it does not return info via libcurl-impersonate, but via original libcurl

withinboredom commented 1 month ago

Can you please share a dockerfile that reproduces the issue and shows what you're trying to do? Trying to piece together one from your post is error-prone.

Is this a docker file that reproduces the issue?

FROM dunglas/frankenphp:1-php8.3 AS php_upstream
RUN git clone https://github.com/lwthiker/curl-impersonate.git
WORKDIR curl-impersonate
RUN mkdir build
WORKDIR build
RUN ../configure
RUN make chrome-build
RUN make chrome-install
RUN ldconfig
WORKDIR /app/curl-impersonate
RUN rm -Rf build

RUN patchelf --set-soname libcurl.so /usr/local/lib/libcurl-impersonate-chrome.so
RUN echo "/usr/local/lib/libcurl-impersonate-chrome.so" > /etc/ld.so.preload
RUN php -r 'print_r(curl_version());'
RUN frankenphp # some script?
dzasa commented 1 month ago

I managed to make it work. Everything is ok with Frankenphp, I just did not realize that frankenphp must be rebuilt after adding curl impersonate.

Here is working dockerfile

FROM dunglas/frankenphp:1.2-builder-php8.3 AS frankenphp_builder

## install curl-imperonate
RUN set -eux; \
    apt-get update && apt-get install --no-install-recommends -y \
        git \
        build-essential \
        pkg-config \
        cmake \
        ninja-build \
        curl \
        autoconf \
        automake \
        libtool \
        golang-go \
        unzip \
        patchelf \
        vim \
    ;

RUN git clone https://github.com/lwthiker/curl-impersonate.git
WORKDIR curl-impersonate
RUN mkdir build
WORKDIR build
RUN ../configure
RUN make chrome-build
RUN make chrome-install
RUN ldconfig
WORKDIR /app/curl-impersonate
RUN rm -Rf build

RUN patchelf --set-soname libcurl.so /usr/local/lib/libcurl-impersonate-chrome.so
RUN echo "/usr/local/lib/libcurl-impersonate-chrome.so" > /etc/ld.so.preload

## rebuild frankenphp
ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS="-DFRANKENPHP_VERSION=$FRANKENPHP_VERSION $PHP_CFLAGS" CGO_CPPFLAGS=$PHP_CPPFLAGS

WORKDIR /go/src/app/caddy/frankenphp
RUN GOBIN=/usr/local/bin go install -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
    setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \
    cp Caddyfile /etc/caddy/Caddyfile && \
    frankenphp version
dunglas commented 1 month ago

That's surprising that you need a full rebuild.

Could you try if cleaning the ldconfig cache works instead?

rm /etc/ld.so.cache
ldconfig
dzasa commented 1 month ago

That worked, thank you :).