Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #450 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added Safari images source code
  • Loading branch information
vania-pooh authored Sep 26, 2021
2 parents 8343342 + 635bcc3 commit 5f06bbc
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 0 deletions.
1 change: 1 addition & 0 deletions static/safari/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd/prism/prism
64 changes: 64 additions & 0 deletions static/safari/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
FROM ubuntu:20.04 as build

ARG WEBKIT_VERSION="610.4.3.1.7"

RUN \
apt-get update && \
apt-get -y install --no-install-recommends ca-certificates subversion && \
svn checkout https://svn.webkit.org/repository/webkit/tags/Safari-$WEBKIT_VERSION webkit && \
mkdir -p /opt/webkit && \
cd webkit && \
yes | DEBIAN_FRONTEND=noninteractive Tools/gtk/install-dependencies && \
cmake -DPORT=GTK -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/webkit -DUSE_WPE_RENDERER=OFF -DENABLE_MINIBROWSER=ON -DENABLE_BUBBLEWRAP_SANDBOX=OFF -DENABLE_GAMEPAD=OFF -DENABLE_SPELLCHECK=OFF -DENABLE_WAYLAND_TARGET=OFF -DUSE_OPENJPEG=OFF -GNinja && \
ninja && \
ninja install && \
rm -Rf /var/lib/apt/lists/*

FROM golang:1.16 as go

COPY cmd/prism /prism

RUN \
apt-get update && \
apt-get install --no-install-recommends -y upx-ucl libx11-dev && \
cd /prism && \
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" && \
upx /prism/prism && \
rm -Rf /var/lib/apt/lists/*

FROM browsers/base:7.2

COPY --from=build /opt/webkit /opt/webkit
COPY --from=go /prism/prism /usr/bin/

ENV LD_LIBRARY_PATH /opt/webkit/lib/:${LD_LIBRARY_PATH}

RUN \
apt-get update && \
apt-get -y install --no-install-recommends \
libsoup2.4-1 \
libgtk-3-0 \
libwebp6 \
libwebpdemux2 \
libsecret-1-0 \
libhyphen0 \
libwoff1 \
libharfbuzz-icu0 \
libgstreamer-gl1.0-0 \
libopenjp2-7 \
libnotify4 \
libxslt1.1 \
libegl1 && \
ldconfig && \
apt-get clean && \
rm -Rf /tmp/* && rm -Rf /var/lib/apt/lists/* && \
chmod 777 /etc/ssl/certs && \
mkdir /tmp/ca-certificates && \
chmod 777 /tmp/ca-certificates

COPY entrypoint.sh /

USER selenium

EXPOSE 4444
ENTRYPOINT ["/entrypoint.sh"]
3 changes: 3 additions & 0 deletions static/safari/cmd/prism/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/aerokube/webkit-images/prism

go 1.16
150 changes: 150 additions & 0 deletions static/safari/cmd/prism/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"path"
"syscall"
"time"
)

var (
listen = ":4444"
target = "http://localhost:5555"
waitTimeout = 30 * time.Second
gracePeriod = 30 * time.Second
browserName = "safari"
browserVersion = "14.0"
)

func wait(ctx context.Context, target string) (*url.URL, error) {
for {
r, err := http.NewRequest(http.MethodHead, target, http.NoBody)
if err != nil {
return nil, fmt.Errorf("new %s request to %s: %v", http.MethodHead, target, err)
}
resp, err := http.DefaultClient.Do(r.WithContext(ctx))
if resp != nil {
resp.Body.Close()
}
if err != nil {
if err, ok := err.(*url.Error); ok {
switch err.Err {
case context.Canceled, context.DeadlineExceeded:
return nil, err
default:
<-time.After(100 * time.Millisecond)
continue
}
}
}
return r.URL, nil
}
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
e := make(chan error)
go func() {
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-e:
log.Fatalf("server: %v", err)
case <-stop:
cancel()
}
}()
waitCtx, waitCancel := context.WithTimeout(ctx, waitTimeout)
defer waitCancel()
u, err := wait(waitCtx, target)
if err != nil {
log.Fatal(fmt.Errorf("wait target: %v", err))
}
server := &http.Server{
Addr: listen,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var value map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&value)
if err != nil && err != io.EOF {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err == nil {
if _, ok := value["desiredCapabilities"]; ok {
delete(value, "desiredCapabilities")
}
if o, ok := value["capabilities"]; ok {
if w3cCapabilities, ok := o.(map[string]interface{}); ok {
for _, match := range []string{"alwaysMatch", "firstMatch"} {
delete(w3cCapabilities, match)
}
}
}
body, err := json.Marshal(value)
if err != nil {
log.Printf("[ERROR] marshalling capabilities: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
r.Body = ioutil.NopCloser(bytes.NewReader(body))
r.ContentLength = int64(len(body))
}
(&httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL.Scheme, r.URL.Host, r.URL.Path = u.Scheme, u.Host, path.Join(u.Path, r.URL.Path)
},
ModifyResponse: func(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
return nil
}
var values map[string]interface{}
defer resp.Body.Close()
err := json.NewDecoder(resp.Body).Decode(&values)
if err != nil {
return fmt.Errorf("decode json response: %v", err)
}
if o, ok := values["value"]; ok {
if value, ok := o.(map[string]interface{}); ok {
if o, ok := value["capabilities"]; ok {
if capabilities, ok := o.(map[string]interface{}); ok {
capabilities["browserName"] = browserName
capabilities["browserVersion"] = browserVersion
delete(capabilities, "platformName")
}
}
}
}
buf, err := json.Marshal(&values)
if err != nil {
return fmt.Errorf("encode json response: %v", err)
}
resp.Header.Del("Server")
resp.Header.Del("Content-Length")
resp.ContentLength = int64(len(buf))
resp.Body = ioutil.NopCloser(bytes.NewReader(buf))
return nil
},
}).ServeHTTP(w, r)
}),
}
go func() {
e <- server.ListenAndServe()
}()
<-ctx.Done()
shCtx, shCancel := context.WithTimeout(context.Background(), gracePeriod)
defer shCancel()
if err := server.Shutdown(shCtx); err != nil {
log.Fatalf("graceful shutdown: %v]", err)
}
}
73 changes: 73 additions & 0 deletions static/safari/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
SCREEN_RESOLUTION=${SCREEN_RESOLUTION:-"1920x1080x24"}
DISPLAY_NUM=99
export DISPLAY=":$DISPLAY_NUM"

QUIET=${QUIET:-""}
DRIVER_ARGS=""
if [ -z "$QUIET" ]; then
DRIVER_ARGS="--verbose"
fi

clean() {
if [ -n "$FILESERVER_PID" ]; then
kill -TERM "$FILESERVER_PID"
fi
if [ -n "$XSELD_PID" ]; then
kill -TERM "$XSELD_PID"
fi
if [ -n "$XVFB_PID" ]; then
kill -TERM "$XVFB_PID"
fi
if [ -n "$DRIVER_PID" ]; then
kill -TERM "$DRIVER_PID"
fi
if [ -n "$PRISM_PID" ]; then
kill -TERM "$PRISM_PID"
fi
if [ -n "$X11VNC_PID" ]; then
kill -TERM "$X11VNC_PID"
fi
}

trap clean SIGINT SIGTERM

/usr/bin/fileserver &
FILESERVER_PID=$!

DISPLAY="$DISPLAY" /usr/bin/xseld &
XSELD_PID=$!

if env | grep -q ROOT_CA_; then
for e in $(env | grep ROOT_CA_ | sed -e 's/=.*$//'); do
certname=$(echo -n $e | sed -e 's/ROOT_CA_//')
echo ${!e} | base64 -d >/tmp/ca-certificates/${certname}.crt
done
update-ca-certificates --localcertsdir /tmp/ca-certificates
fi

/usr/bin/xvfb-run -l -n "$DISPLAY_NUM" -s "-ac -screen 0 $SCREEN_RESOLUTION -noreset -listen tcp" /usr/bin/fluxbox -display "$DISPLAY" >/dev/null 2>&1 &
XVFB_PID=$!

retcode=1
until [ $retcode -eq 0 ]; do
DISPLAY="$DISPLAY" wmctrl -m >/dev/null 2>&1
retcode=$?
if [ $retcode -ne 0 ]; then
echo Waiting X server...
sleep 0.1
fi
done

if [ "$ENABLE_VNC" == "true" ]; then
x11vnc -display "$DISPLAY" -passwd selenoid -shared -forever -loop500 -rfbport 5900 -rfbportv6 5900 -logfile /dev/null &
X11VNC_PID=$!
fi

DISPLAY="$DISPLAY" /opt/webkit/bin/WebKitWebDriver --port=5555 --host=0.0.0.0 ${DRIVER_ARGS} &
DRIVER_PID=$!

/usr/bin/prism &
PRISM_PID=$!

wait

0 comments on commit 5f06bbc

Please sign in to comment.