Algorithms – Find portions of line segments close to each other

I have two line segments in 3D space. The line segments are defined by 2 points each: A1, B1 and A2, B2 (see left part in the diagram below).

I would like to find an algorithm to detect which part of the line segment 1 has a distance <= x to the line segment 2 (and the inverse). The expected result of the algorithm is represented by the red lines in the diagram:

enter the description of the image here

Could you please give me some tips and tricks? I do not know where to start. Thank you.